cmListFileCache.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 <sstream>
  13. struct cmListFileParser
  14. {
  15. cmListFileParser(cmListFile* lf, cmListFileBacktrace const& lfbt,
  16. cmMessenger* messenger, const char* filename);
  17. ~cmListFileParser();
  18. void IssueFileOpenError(std::string const& text) const;
  19. void IssueError(std::string const& text) const;
  20. bool ParseFile();
  21. bool ParseFunction(const char* name, long line);
  22. bool AddArgument(cmListFileLexer_Token* token,
  23. cmListFileArgument::Delimiter delim);
  24. cmListFile* ListFile;
  25. cmListFileBacktrace Backtrace;
  26. cmMessenger* Messenger;
  27. const char* FileName;
  28. cmListFileLexer* Lexer;
  29. cmListFileFunction Function;
  30. enum
  31. {
  32. SeparationOkay,
  33. SeparationWarning,
  34. SeparationError
  35. } Separation;
  36. };
  37. cmListFileParser::cmListFileParser(cmListFile* lf,
  38. cmListFileBacktrace const& 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, nullptr, 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, nullptr, 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. this->Function.Arguments.emplace_back(token->text, delim, token->line);
  236. if (this->Separation == SeparationOkay) {
  237. return true;
  238. }
  239. bool isError = (this->Separation == SeparationError ||
  240. delim == cmListFileArgument::Bracket);
  241. std::ostringstream m;
  242. cmListFileContext lfc;
  243. lfc.FilePath = this->FileName;
  244. lfc.Line = token->line;
  245. cmListFileBacktrace lfbt = this->Backtrace;
  246. lfbt = lfbt.Push(lfc);
  247. m << "Syntax " << (isError ? "Error" : "Warning") << " in cmake code at "
  248. << "column " << token->column << "\n"
  249. << "Argument not separated from preceding token by whitespace.";
  250. /* clang-format on */
  251. if (isError) {
  252. this->Messenger->IssueMessage(cmake::FATAL_ERROR, m.str(), lfbt);
  253. return false;
  254. }
  255. this->Messenger->IssueMessage(cmake::AUTHOR_WARNING, m.str(), lfbt);
  256. return true;
  257. }
  258. struct cmListFileBacktrace::Entry : public cmListFileContext
  259. {
  260. Entry(cmListFileContext const& lfc, Entry* up)
  261. : cmListFileContext(lfc)
  262. , Up(up)
  263. , RefCount(0)
  264. {
  265. if (this->Up) {
  266. this->Up->Ref();
  267. }
  268. }
  269. ~Entry()
  270. {
  271. if (this->Up) {
  272. this->Up->Unref();
  273. }
  274. }
  275. void Ref() { ++this->RefCount; }
  276. void Unref()
  277. {
  278. if (--this->RefCount == 0) {
  279. delete this;
  280. }
  281. }
  282. Entry* Up;
  283. unsigned int RefCount;
  284. };
  285. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& bottom,
  286. 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 const& bottom,
  295. Entry* cur)
  296. : Bottom(bottom)
  297. , Cur(cur)
  298. {
  299. if (this->Cur) {
  300. assert(this->Bottom.IsValid());
  301. this->Cur->Ref();
  302. }
  303. }
  304. cmListFileBacktrace::cmListFileBacktrace()
  305. : Bottom()
  306. , Cur(nullptr)
  307. {
  308. }
  309. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& snapshot)
  310. : Bottom(snapshot.GetCallStackBottom())
  311. , Cur(nullptr)
  312. {
  313. }
  314. cmListFileBacktrace::cmListFileBacktrace(cmListFileBacktrace const& r)
  315. : Bottom(r.Bottom)
  316. , Cur(r.Cur)
  317. {
  318. if (this->Cur) {
  319. assert(this->Bottom.IsValid());
  320. this->Cur->Ref();
  321. }
  322. }
  323. cmListFileBacktrace& cmListFileBacktrace::operator=(
  324. cmListFileBacktrace const& r)
  325. {
  326. cmListFileBacktrace tmp(r);
  327. std::swap(this->Cur, tmp.Cur);
  328. std::swap(this->Bottom, tmp.Bottom);
  329. return *this;
  330. }
  331. cmListFileBacktrace::~cmListFileBacktrace()
  332. {
  333. if (this->Cur) {
  334. this->Cur->Unref();
  335. }
  336. }
  337. cmListFileBacktrace cmListFileBacktrace::Push(std::string const& file) const
  338. {
  339. // We are entering a file-level scope but have not yet reached
  340. // any specific line or command invocation within it. This context
  341. // is useful to print when it is at the top but otherwise can be
  342. // skipped during call stack printing.
  343. cmListFileContext lfc;
  344. lfc.FilePath = file;
  345. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  346. }
  347. cmListFileBacktrace cmListFileBacktrace::Push(
  348. cmListFileContext const& lfc) const
  349. {
  350. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  351. }
  352. cmListFileBacktrace cmListFileBacktrace::Pop() const
  353. {
  354. assert(this->Cur);
  355. return cmListFileBacktrace(this->Bottom, this->Cur->Up);
  356. }
  357. cmListFileContext const& cmListFileBacktrace::Top() const
  358. {
  359. if (this->Cur) {
  360. return *this->Cur;
  361. }
  362. static cmListFileContext const empty;
  363. return empty;
  364. }
  365. void cmListFileBacktrace::PrintTitle(std::ostream& out) const
  366. {
  367. if (!this->Cur) {
  368. return;
  369. }
  370. cmOutputConverter converter(this->Bottom);
  371. cmListFileContext lfc = *this->Cur;
  372. if (!this->Bottom.GetState()->GetIsInTryCompile()) {
  373. lfc.FilePath = converter.ConvertToRelativePath(
  374. this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  375. }
  376. out << (lfc.Line ? " at " : " in ") << lfc;
  377. }
  378. void cmListFileBacktrace::PrintCallStack(std::ostream& out) const
  379. {
  380. if (!this->Cur || !this->Cur->Up) {
  381. return;
  382. }
  383. bool first = true;
  384. cmOutputConverter converter(this->Bottom);
  385. for (Entry* i = this->Cur->Up; i; i = i->Up) {
  386. if (i->Name.empty()) {
  387. // Skip this whole-file scope. When we get here we already will
  388. // have printed a more-specific context within the file.
  389. continue;
  390. }
  391. if (first) {
  392. first = false;
  393. out << "Call Stack (most recent call first):\n";
  394. }
  395. cmListFileContext lfc = *i;
  396. if (!this->Bottom.GetState()->GetIsInTryCompile()) {
  397. lfc.FilePath = converter.ConvertToRelativePath(
  398. this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  399. }
  400. out << " " << lfc << "\n";
  401. }
  402. }
  403. size_t cmListFileBacktrace::Depth() const
  404. {
  405. size_t depth = 0;
  406. if (this->Cur == nullptr) {
  407. return 0;
  408. }
  409. for (Entry* i = this->Cur->Up; i; i = i->Up) {
  410. depth++;
  411. }
  412. return depth;
  413. }
  414. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  415. {
  416. os << lfc.FilePath;
  417. if (lfc.Line) {
  418. os << ":" << lfc.Line;
  419. if (!lfc.Name.empty()) {
  420. os << " (" << lfc.Name << ")";
  421. }
  422. }
  423. return os;
  424. }
  425. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs)
  426. {
  427. if (lhs.Line != rhs.Line) {
  428. return lhs.Line < rhs.Line;
  429. }
  430. return lhs.FilePath < rhs.FilePath;
  431. }
  432. bool operator==(const cmListFileContext& lhs, const cmListFileContext& rhs)
  433. {
  434. return lhs.Line == rhs.Line && lhs.FilePath == rhs.FilePath;
  435. }
  436. bool operator!=(const cmListFileContext& lhs, const cmListFileContext& rhs)
  437. {
  438. return !(lhs == rhs);
  439. }