cmListFileCache.cxx 14 KB

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