cmListFileCache.cxx 14 KB

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