cmListFileCache.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmListFileCache.h"
  4. #include "cmListFileLexer.h"
  5. #include "cmMessenger.h"
  6. #include "cmOutputConverter.h"
  7. #include "cmState.h"
  8. #include "cmSystemTools.h"
  9. #include "cmake.h"
  10. #include <algorithm>
  11. #include <assert.h>
  12. #include <cmConfigure.h>
  13. #include <sstream>
  14. struct cmListFileParser
  15. {
  16. cmListFileParser(cmListFile* lf, cmListFileBacktrace lfbt,
  17. cmMessenger* messenger, const char* filename);
  18. ~cmListFileParser();
  19. void IssueFileOpenError(std::string const& text) const;
  20. void IssueError(std::string const& text) const;
  21. bool ParseFile();
  22. bool ParseFunction(const char* name, long line);
  23. bool AddArgument(cmListFileLexer_Token* token,
  24. cmListFileArgument::Delimiter delim);
  25. cmListFile* ListFile;
  26. cmListFileBacktrace Backtrace;
  27. cmMessenger* Messenger;
  28. const char* FileName;
  29. cmListFileLexer* Lexer;
  30. cmListFileFunction Function;
  31. enum
  32. {
  33. SeparationOkay,
  34. SeparationWarning,
  35. SeparationError
  36. } Separation;
  37. };
  38. cmListFileParser::cmListFileParser(cmListFile* lf, cmListFileBacktrace lfbt,
  39. cmMessenger* messenger,
  40. const char* filename)
  41. : ListFile(lf)
  42. , Backtrace(lfbt)
  43. , Messenger(messenger)
  44. , FileName(filename)
  45. , Lexer(cmListFileLexer_New())
  46. {
  47. }
  48. cmListFileParser::~cmListFileParser()
  49. {
  50. cmListFileLexer_Delete(this->Lexer);
  51. }
  52. void cmListFileParser::IssueFileOpenError(const std::string& text) const
  53. {
  54. this->Messenger->IssueMessage(cmake::FATAL_ERROR, text, this->Backtrace);
  55. }
  56. void cmListFileParser::IssueError(const std::string& text) const
  57. {
  58. cmListFileContext lfc;
  59. lfc.FilePath = this->FileName;
  60. lfc.Line = cmListFileLexer_GetCurrentLine(this->Lexer);
  61. cmListFileBacktrace lfbt = this->Backtrace;
  62. lfbt = lfbt.Push(lfc);
  63. this->Messenger->IssueMessage(cmake::FATAL_ERROR, text, lfbt);
  64. cmSystemTools::SetFatalErrorOccured();
  65. }
  66. bool cmListFileParser::ParseFile()
  67. {
  68. // Open the file.
  69. cmListFileLexer_BOM bom;
  70. if (!cmListFileLexer_SetFileName(this->Lexer, this->FileName, &bom)) {
  71. this->IssueFileOpenError("cmListFileCache: error can not open file.");
  72. return false;
  73. }
  74. if (bom == cmListFileLexer_BOM_Broken) {
  75. cmListFileLexer_SetFileName(this->Lexer, CM_NULLPTR, CM_NULLPTR);
  76. this->IssueFileOpenError("Error while reading Byte-Order-Mark. "
  77. "File not seekable?");
  78. return false;
  79. }
  80. // Verify the Byte-Order-Mark, if any.
  81. if (bom != cmListFileLexer_BOM_None && bom != cmListFileLexer_BOM_UTF8) {
  82. cmListFileLexer_SetFileName(this->Lexer, CM_NULLPTR, CM_NULLPTR);
  83. this->IssueFileOpenError(
  84. "File starts with a Byte-Order-Mark that is not UTF-8.");
  85. return false;
  86. }
  87. // Use a simple recursive-descent parser to process the token
  88. // stream.
  89. bool haveNewline = true;
  90. while (cmListFileLexer_Token* token = cmListFileLexer_Scan(this->Lexer)) {
  91. if (token->type == cmListFileLexer_Token_Space) {
  92. } else if (token->type == cmListFileLexer_Token_Newline) {
  93. haveNewline = true;
  94. } else if (token->type == cmListFileLexer_Token_CommentBracket) {
  95. haveNewline = false;
  96. } else if (token->type == cmListFileLexer_Token_Identifier) {
  97. if (haveNewline) {
  98. haveNewline = false;
  99. if (this->ParseFunction(token->text, token->line)) {
  100. this->ListFile->Functions.push_back(this->Function);
  101. } else {
  102. return false;
  103. }
  104. } else {
  105. std::ostringstream error;
  106. error << "Parse error. Expected a newline, got "
  107. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  108. << " with text \"" << token->text << "\".";
  109. this->IssueError(error.str());
  110. return false;
  111. }
  112. } else {
  113. std::ostringstream error;
  114. error << "Parse error. Expected a command name, got "
  115. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  116. << " with text \"" << token->text << "\".";
  117. this->IssueError(error.str());
  118. return false;
  119. }
  120. }
  121. return true;
  122. }
  123. bool cmListFile::ParseFile(const char* filename, cmMessenger* messenger,
  124. cmListFileBacktrace const& lfbt)
  125. {
  126. if (!cmSystemTools::FileExists(filename) ||
  127. cmSystemTools::FileIsDirectory(filename)) {
  128. return false;
  129. }
  130. bool parseError = false;
  131. {
  132. cmListFileParser parser(this, lfbt, messenger, filename);
  133. parseError = !parser.ParseFile();
  134. }
  135. return !parseError;
  136. }
  137. bool cmListFileParser::ParseFunction(const char* name, long line)
  138. {
  139. // Ininitialize a new function call.
  140. this->Function = cmListFileFunction();
  141. this->Function.Name = name;
  142. this->Function.Line = line;
  143. // Command name has already been parsed. Read the left paren.
  144. cmListFileLexer_Token* token;
  145. while ((token = cmListFileLexer_Scan(this->Lexer)) &&
  146. token->type == cmListFileLexer_Token_Space) {
  147. }
  148. if (!token) {
  149. std::ostringstream error;
  150. /* clang-format off */
  151. error << "Unexpected end of file.\n"
  152. << "Parse error. Function missing opening \"(\".";
  153. /* clang-format on */
  154. this->IssueError(error.str());
  155. return false;
  156. }
  157. if (token->type != cmListFileLexer_Token_ParenLeft) {
  158. std::ostringstream error;
  159. error << "Parse error. Expected \"(\", got "
  160. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  161. << " with text \"" << token->text << "\".";
  162. this->IssueError(error.str());
  163. return false;
  164. }
  165. // Arguments.
  166. unsigned long lastLine;
  167. unsigned long parenDepth = 0;
  168. this->Separation = SeparationOkay;
  169. while ((lastLine = cmListFileLexer_GetCurrentLine(this->Lexer),
  170. token = cmListFileLexer_Scan(this->Lexer))) {
  171. if (token->type == cmListFileLexer_Token_Space ||
  172. token->type == cmListFileLexer_Token_Newline) {
  173. this->Separation = SeparationOkay;
  174. continue;
  175. }
  176. if (token->type == cmListFileLexer_Token_ParenLeft) {
  177. parenDepth++;
  178. this->Separation = SeparationOkay;
  179. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  180. return false;
  181. }
  182. } else if (token->type == cmListFileLexer_Token_ParenRight) {
  183. if (parenDepth == 0) {
  184. return true;
  185. }
  186. parenDepth--;
  187. this->Separation = SeparationOkay;
  188. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  189. return false;
  190. }
  191. this->Separation = SeparationWarning;
  192. } else if (token->type == cmListFileLexer_Token_Identifier ||
  193. token->type == cmListFileLexer_Token_ArgumentUnquoted) {
  194. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  195. return false;
  196. }
  197. this->Separation = SeparationWarning;
  198. } else if (token->type == cmListFileLexer_Token_ArgumentQuoted) {
  199. if (!this->AddArgument(token, cmListFileArgument::Quoted)) {
  200. return false;
  201. }
  202. this->Separation = SeparationWarning;
  203. } else if (token->type == cmListFileLexer_Token_ArgumentBracket) {
  204. if (!this->AddArgument(token, cmListFileArgument::Bracket)) {
  205. return false;
  206. }
  207. this->Separation = SeparationError;
  208. } else if (token->type == cmListFileLexer_Token_CommentBracket) {
  209. this->Separation = SeparationError;
  210. } else {
  211. // Error.
  212. std::ostringstream error;
  213. error << "Parse error. Function missing ending \")\". "
  214. << "Instead found "
  215. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  216. << " with text \"" << token->text << "\".";
  217. this->IssueError(error.str());
  218. return false;
  219. }
  220. }
  221. std::ostringstream error;
  222. cmListFileContext lfc;
  223. lfc.FilePath = this->FileName;
  224. lfc.Line = lastLine;
  225. cmListFileBacktrace lfbt = this->Backtrace;
  226. lfbt = lfbt.Push(lfc);
  227. error << "Parse error. Function missing ending \")\". "
  228. << "End of file reached.";
  229. this->Messenger->IssueMessage(cmake::FATAL_ERROR, error.str(), lfbt);
  230. return false;
  231. }
  232. bool cmListFileParser::AddArgument(cmListFileLexer_Token* token,
  233. cmListFileArgument::Delimiter delim)
  234. {
  235. cmListFileArgument a(token->text, delim, token->line);
  236. this->Function.Arguments.push_back(a);
  237. if (this->Separation == SeparationOkay) {
  238. return true;
  239. }
  240. bool isError = (this->Separation == SeparationError ||
  241. delim == cmListFileArgument::Bracket);
  242. std::ostringstream m;
  243. cmListFileContext lfc;
  244. lfc.FilePath = this->FileName;
  245. lfc.Line = token->line;
  246. cmListFileBacktrace lfbt = this->Backtrace;
  247. lfbt = lfbt.Push(lfc);
  248. m << "Syntax " << (isError ? "Error" : "Warning") << " in cmake code at "
  249. << "column " << token->column << "\n"
  250. << "Argument not separated from preceding token by whitespace.";
  251. /* clang-format on */
  252. if (isError) {
  253. this->Messenger->IssueMessage(cmake::FATAL_ERROR, m.str(), lfbt);
  254. return false;
  255. }
  256. this->Messenger->IssueMessage(cmake::AUTHOR_WARNING, m.str(), lfbt);
  257. return true;
  258. }
  259. struct cmListFileBacktrace::Entry : public cmListFileContext
  260. {
  261. Entry(cmListFileContext const& lfc, Entry* up)
  262. : cmListFileContext(lfc)
  263. , Up(up)
  264. , RefCount(0)
  265. {
  266. if (this->Up) {
  267. this->Up->Ref();
  268. }
  269. }
  270. ~Entry()
  271. {
  272. if (this->Up) {
  273. this->Up->Unref();
  274. }
  275. }
  276. void Ref() { ++this->RefCount; }
  277. void Unref()
  278. {
  279. if (--this->RefCount == 0) {
  280. delete this;
  281. }
  282. }
  283. Entry* Up;
  284. unsigned int RefCount;
  285. };
  286. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot bottom, Entry* up,
  287. cmListFileContext const& lfc)
  288. : Bottom(bottom)
  289. , Cur(new Entry(lfc, up))
  290. {
  291. assert(this->Bottom.IsValid());
  292. this->Cur->Ref();
  293. }
  294. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot bottom, Entry* cur)
  295. : Bottom(bottom)
  296. , Cur(cur)
  297. {
  298. if (this->Cur) {
  299. assert(this->Bottom.IsValid());
  300. this->Cur->Ref();
  301. }
  302. }
  303. cmListFileBacktrace::cmListFileBacktrace()
  304. : Bottom()
  305. , Cur(CM_NULLPTR)
  306. {
  307. }
  308. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot snapshot)
  309. : Bottom(snapshot.GetCallStackBottom())
  310. , Cur(CM_NULLPTR)
  311. {
  312. }
  313. cmListFileBacktrace::cmListFileBacktrace(cmListFileBacktrace const& r)
  314. : Bottom(r.Bottom)
  315. , Cur(r.Cur)
  316. {
  317. if (this->Cur) {
  318. assert(this->Bottom.IsValid());
  319. this->Cur->Ref();
  320. }
  321. }
  322. cmListFileBacktrace& cmListFileBacktrace::operator=(
  323. cmListFileBacktrace const& r)
  324. {
  325. cmListFileBacktrace tmp(r);
  326. std::swap(this->Cur, tmp.Cur);
  327. std::swap(this->Bottom, tmp.Bottom);
  328. return *this;
  329. }
  330. cmListFileBacktrace::~cmListFileBacktrace()
  331. {
  332. if (this->Cur) {
  333. this->Cur->Unref();
  334. }
  335. }
  336. cmListFileBacktrace cmListFileBacktrace::Push(std::string const& file) const
  337. {
  338. // We are entering a file-level scope but have not yet reached
  339. // any specific line or command invocation within it. This context
  340. // is useful to print when it is at the top but otherwise can be
  341. // skipped during call stack printing.
  342. cmListFileContext lfc;
  343. lfc.FilePath = file;
  344. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  345. }
  346. cmListFileBacktrace cmListFileBacktrace::Push(
  347. cmListFileContext const& lfc) const
  348. {
  349. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  350. }
  351. cmListFileBacktrace cmListFileBacktrace::Pop() const
  352. {
  353. assert(this->Cur);
  354. return cmListFileBacktrace(this->Bottom, this->Cur->Up);
  355. }
  356. cmListFileContext const& cmListFileBacktrace::Top() const
  357. {
  358. if (this->Cur) {
  359. return *this->Cur;
  360. }
  361. static cmListFileContext const empty;
  362. return empty;
  363. }
  364. void cmListFileBacktrace::PrintTitle(std::ostream& out) const
  365. {
  366. if (!this->Cur) {
  367. return;
  368. }
  369. cmOutputConverter converter(this->Bottom);
  370. cmListFileContext lfc = *this->Cur;
  371. if (!this->Bottom.GetState()->GetIsInTryCompile()) {
  372. lfc.FilePath = converter.ConvertToRelativePath(
  373. this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  374. }
  375. out << (lfc.Line ? " at " : " in ") << lfc;
  376. }
  377. void cmListFileBacktrace::PrintCallStack(std::ostream& out) const
  378. {
  379. if (!this->Cur || !this->Cur->Up) {
  380. return;
  381. }
  382. bool first = true;
  383. cmOutputConverter converter(this->Bottom);
  384. for (Entry* i = this->Cur->Up; i; i = i->Up) {
  385. if (i->Name.empty()) {
  386. // Skip this whole-file scope. When we get here we already will
  387. // have printed a more-specific context within the file.
  388. continue;
  389. }
  390. if (first) {
  391. first = false;
  392. out << "Call Stack (most recent call first):\n";
  393. }
  394. cmListFileContext lfc = *i;
  395. if (!this->Bottom.GetState()->GetIsInTryCompile()) {
  396. lfc.FilePath = converter.ConvertToRelativePath(
  397. this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  398. }
  399. out << " " << lfc << "\n";
  400. }
  401. }
  402. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  403. {
  404. os << lfc.FilePath;
  405. if (lfc.Line) {
  406. os << ":" << lfc.Line;
  407. if (!lfc.Name.empty()) {
  408. os << " (" << lfc.Name << ")";
  409. }
  410. }
  411. return os;
  412. }
  413. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs)
  414. {
  415. if (lhs.Line != rhs.Line) {
  416. return lhs.Line < rhs.Line;
  417. }
  418. return lhs.FilePath < rhs.FilePath;
  419. }
  420. bool operator==(const cmListFileContext& lhs, const cmListFileContext& rhs)
  421. {
  422. return lhs.Line == rhs.Line && lhs.FilePath == rhs.FilePath;
  423. }
  424. bool operator!=(const cmListFileContext& lhs, const cmListFileContext& rhs)
  425. {
  426. return !(lhs == rhs);
  427. }