cmListFileCache.cxx 15 KB

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