cmListFileCache.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. 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 const& bottom,
  287. Entry* up,
  288. cmListFileContext const& lfc)
  289. : Bottom(bottom)
  290. , Cur(new Entry(lfc, up))
  291. {
  292. assert(this->Bottom.IsValid());
  293. this->Cur->Ref();
  294. }
  295. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& bottom,
  296. Entry* cur)
  297. : Bottom(bottom)
  298. , Cur(cur)
  299. {
  300. if (this->Cur) {
  301. assert(this->Bottom.IsValid());
  302. this->Cur->Ref();
  303. }
  304. }
  305. cmListFileBacktrace::cmListFileBacktrace()
  306. : Bottom()
  307. , Cur(nullptr)
  308. {
  309. }
  310. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& snapshot)
  311. : Bottom(snapshot.GetCallStackBottom())
  312. , Cur(nullptr)
  313. {
  314. }
  315. cmListFileBacktrace::cmListFileBacktrace(cmListFileBacktrace const& r)
  316. : Bottom(r.Bottom)
  317. , Cur(r.Cur)
  318. {
  319. if (this->Cur) {
  320. assert(this->Bottom.IsValid());
  321. this->Cur->Ref();
  322. }
  323. }
  324. cmListFileBacktrace& cmListFileBacktrace::operator=(
  325. cmListFileBacktrace const& r)
  326. {
  327. cmListFileBacktrace tmp(r);
  328. std::swap(this->Cur, tmp.Cur);
  329. std::swap(this->Bottom, tmp.Bottom);
  330. return *this;
  331. }
  332. cmListFileBacktrace::~cmListFileBacktrace()
  333. {
  334. if (this->Cur) {
  335. this->Cur->Unref();
  336. }
  337. }
  338. cmListFileBacktrace cmListFileBacktrace::Push(std::string const& file) const
  339. {
  340. // We are entering a file-level scope but have not yet reached
  341. // any specific line or command invocation within it. This context
  342. // is useful to print when it is at the top but otherwise can be
  343. // skipped during call stack printing.
  344. cmListFileContext lfc;
  345. lfc.FilePath = file;
  346. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  347. }
  348. cmListFileBacktrace cmListFileBacktrace::Push(
  349. cmListFileContext const& lfc) const
  350. {
  351. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  352. }
  353. cmListFileBacktrace cmListFileBacktrace::Pop() const
  354. {
  355. assert(this->Cur);
  356. return cmListFileBacktrace(this->Bottom, this->Cur->Up);
  357. }
  358. cmListFileContext const& cmListFileBacktrace::Top() const
  359. {
  360. if (this->Cur) {
  361. return *this->Cur;
  362. }
  363. static cmListFileContext const empty;
  364. return empty;
  365. }
  366. void cmListFileBacktrace::PrintTitle(std::ostream& out) const
  367. {
  368. if (!this->Cur) {
  369. return;
  370. }
  371. cmOutputConverter converter(this->Bottom);
  372. cmListFileContext lfc = *this->Cur;
  373. if (!this->Bottom.GetState()->GetIsInTryCompile()) {
  374. lfc.FilePath = converter.ConvertToRelativePath(
  375. this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  376. }
  377. out << (lfc.Line ? " at " : " in ") << lfc;
  378. }
  379. void cmListFileBacktrace::PrintCallStack(std::ostream& out) const
  380. {
  381. if (!this->Cur || !this->Cur->Up) {
  382. return;
  383. }
  384. bool first = true;
  385. cmOutputConverter converter(this->Bottom);
  386. for (Entry* i = this->Cur->Up; i; i = i->Up) {
  387. if (i->Name.empty()) {
  388. // Skip this whole-file scope. When we get here we already will
  389. // have printed a more-specific context within the file.
  390. continue;
  391. }
  392. if (first) {
  393. first = false;
  394. out << "Call Stack (most recent call first):\n";
  395. }
  396. cmListFileContext lfc = *i;
  397. if (!this->Bottom.GetState()->GetIsInTryCompile()) {
  398. lfc.FilePath = converter.ConvertToRelativePath(
  399. this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  400. }
  401. out << " " << lfc << "\n";
  402. }
  403. }
  404. size_t cmListFileBacktrace::Depth() const
  405. {
  406. size_t depth = 0;
  407. if (this->Cur == nullptr) {
  408. return 0;
  409. }
  410. for (Entry* i = this->Cur->Up; i; i = i->Up) {
  411. depth++;
  412. }
  413. return depth;
  414. }
  415. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  416. {
  417. os << lfc.FilePath;
  418. if (lfc.Line) {
  419. os << ":" << lfc.Line;
  420. if (!lfc.Name.empty()) {
  421. os << " (" << lfc.Name << ")";
  422. }
  423. }
  424. return os;
  425. }
  426. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs)
  427. {
  428. if (lhs.Line != rhs.Line) {
  429. return lhs.Line < rhs.Line;
  430. }
  431. return lhs.FilePath < rhs.FilePath;
  432. }
  433. bool operator==(const cmListFileContext& lhs, const cmListFileContext& rhs)
  434. {
  435. return lhs.Line == rhs.Line && lhs.FilePath == rhs.FilePath;
  436. }
  437. bool operator!=(const cmListFileContext& lhs, const cmListFileContext& rhs)
  438. {
  439. return !(lhs == rhs);
  440. }