cmListFileCache.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 <memory>
  13. #include <sstream>
  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(cmake::FATAL_ERROR, text, this->Backtrace);
  63. }
  64. void cmListFileParser::IssueError(const std::string& text) const
  65. {
  66. cmListFileContext lfc;
  67. lfc.FilePath = this->FileName;
  68. lfc.Line = cmListFileLexer_GetCurrentLine(this->Lexer);
  69. cmListFileBacktrace lfbt = this->Backtrace;
  70. lfbt = lfbt.Push(lfc);
  71. this->Messenger->IssueMessage(cmake::FATAL_ERROR, text, lfbt);
  72. cmSystemTools::SetFatalErrorOccured();
  73. }
  74. bool cmListFileParser::ParseFile()
  75. {
  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. // Use a simple recursive-descent parser to process the token
  96. // stream.
  97. bool haveNewline = true;
  98. while (cmListFileLexer_Token* token = cmListFileLexer_Scan(this->Lexer)) {
  99. if (token->type == cmListFileLexer_Token_Space) {
  100. } else if (token->type == cmListFileLexer_Token_Newline) {
  101. haveNewline = true;
  102. } else if (token->type == cmListFileLexer_Token_CommentBracket) {
  103. haveNewline = false;
  104. } else if (token->type == cmListFileLexer_Token_Identifier) {
  105. if (haveNewline) {
  106. haveNewline = false;
  107. if (this->ParseFunction(token->text, token->line)) {
  108. this->ListFile->Functions.push_back(this->Function);
  109. } else {
  110. return false;
  111. }
  112. } else {
  113. std::ostringstream error;
  114. error << "Parse error. Expected a newline, got "
  115. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  116. << " with text \"" << token->text << "\".";
  117. this->IssueError(error.str());
  118. return false;
  119. }
  120. } else {
  121. std::ostringstream error;
  122. error << "Parse error. Expected a command name, got "
  123. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  124. << " with text \"" << token->text << "\".";
  125. this->IssueError(error.str());
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. bool cmListFile::ParseFile(const char* filename, cmMessenger* messenger,
  132. cmListFileBacktrace const& lfbt)
  133. {
  134. if (!cmSystemTools::FileExists(filename) ||
  135. cmSystemTools::FileIsDirectory(filename)) {
  136. return false;
  137. }
  138. bool parseError = false;
  139. {
  140. cmListFileParser parser(this, lfbt, messenger, filename);
  141. parseError = !parser.ParseFile();
  142. }
  143. return !parseError;
  144. }
  145. bool cmListFileParser::ParseFunction(const char* name, long line)
  146. {
  147. // Ininitialize a new function call.
  148. this->Function = cmListFileFunction();
  149. this->Function.Name = name;
  150. this->Function.Line = line;
  151. // Command name has already been parsed. Read the left paren.
  152. cmListFileLexer_Token* token;
  153. while ((token = cmListFileLexer_Scan(this->Lexer)) &&
  154. token->type == cmListFileLexer_Token_Space) {
  155. }
  156. if (!token) {
  157. std::ostringstream error;
  158. /* clang-format off */
  159. error << "Unexpected end of file.\n"
  160. << "Parse error. Function missing opening \"(\".";
  161. /* clang-format on */
  162. this->IssueError(error.str());
  163. return false;
  164. }
  165. if (token->type != cmListFileLexer_Token_ParenLeft) {
  166. std::ostringstream error;
  167. error << "Parse error. Expected \"(\", got "
  168. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  169. << " with text \"" << token->text << "\".";
  170. this->IssueError(error.str());
  171. return false;
  172. }
  173. // Arguments.
  174. unsigned long lastLine;
  175. unsigned long parenDepth = 0;
  176. this->Separation = SeparationOkay;
  177. while ((lastLine = cmListFileLexer_GetCurrentLine(this->Lexer),
  178. token = cmListFileLexer_Scan(this->Lexer))) {
  179. if (token->type == cmListFileLexer_Token_Space ||
  180. token->type == cmListFileLexer_Token_Newline) {
  181. this->Separation = SeparationOkay;
  182. continue;
  183. }
  184. if (token->type == cmListFileLexer_Token_ParenLeft) {
  185. parenDepth++;
  186. this->Separation = SeparationOkay;
  187. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  188. return false;
  189. }
  190. } else if (token->type == cmListFileLexer_Token_ParenRight) {
  191. if (parenDepth == 0) {
  192. return true;
  193. }
  194. parenDepth--;
  195. this->Separation = SeparationOkay;
  196. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  197. return false;
  198. }
  199. this->Separation = SeparationWarning;
  200. } else if (token->type == cmListFileLexer_Token_Identifier ||
  201. token->type == cmListFileLexer_Token_ArgumentUnquoted) {
  202. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  203. return false;
  204. }
  205. this->Separation = SeparationWarning;
  206. } else if (token->type == cmListFileLexer_Token_ArgumentQuoted) {
  207. if (!this->AddArgument(token, cmListFileArgument::Quoted)) {
  208. return false;
  209. }
  210. this->Separation = SeparationWarning;
  211. } else if (token->type == cmListFileLexer_Token_ArgumentBracket) {
  212. if (!this->AddArgument(token, cmListFileArgument::Bracket)) {
  213. return false;
  214. }
  215. this->Separation = SeparationError;
  216. } else if (token->type == cmListFileLexer_Token_CommentBracket) {
  217. this->Separation = SeparationError;
  218. } else {
  219. // Error.
  220. std::ostringstream error;
  221. error << "Parse error. Function missing ending \")\". "
  222. << "Instead found "
  223. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  224. << " with text \"" << token->text << "\".";
  225. this->IssueError(error.str());
  226. return false;
  227. }
  228. }
  229. std::ostringstream error;
  230. cmListFileContext lfc;
  231. lfc.FilePath = this->FileName;
  232. lfc.Line = lastLine;
  233. cmListFileBacktrace lfbt = this->Backtrace;
  234. lfbt = lfbt.Push(lfc);
  235. error << "Parse error. Function missing ending \")\". "
  236. << "End of file reached.";
  237. this->Messenger->IssueMessage(cmake::FATAL_ERROR, error.str(), lfbt);
  238. return false;
  239. }
  240. bool cmListFileParser::AddArgument(cmListFileLexer_Token* token,
  241. cmListFileArgument::Delimiter delim)
  242. {
  243. this->Function.Arguments.emplace_back(token->text, delim, token->line);
  244. if (this->Separation == SeparationOkay) {
  245. return true;
  246. }
  247. bool isError = (this->Separation == SeparationError ||
  248. delim == cmListFileArgument::Bracket);
  249. std::ostringstream m;
  250. cmListFileContext lfc;
  251. lfc.FilePath = this->FileName;
  252. lfc.Line = token->line;
  253. cmListFileBacktrace lfbt = this->Backtrace;
  254. lfbt = lfbt.Push(lfc);
  255. m << "Syntax " << (isError ? "Error" : "Warning") << " in cmake code at "
  256. << "column " << token->column << "\n"
  257. << "Argument not separated from preceding token by whitespace.";
  258. /* clang-format on */
  259. if (isError) {
  260. this->Messenger->IssueMessage(cmake::FATAL_ERROR, m.str(), lfbt);
  261. return false;
  262. }
  263. this->Messenger->IssueMessage(cmake::AUTHOR_WARNING, m.str(), lfbt);
  264. return true;
  265. }
  266. // We hold either the bottom scope of a directory or a call/file context.
  267. // Discriminate these cases via the parent pointer.
  268. struct cmListFileBacktrace::Entry
  269. {
  270. Entry(cmStateSnapshot bottom)
  271. : Bottom(bottom)
  272. {
  273. }
  274. Entry(std::shared_ptr<Entry const> parent, cmListFileContext lfc)
  275. : Context(std::move(lfc))
  276. , Parent(std::move(parent))
  277. {
  278. }
  279. ~Entry()
  280. {
  281. if (this->Parent) {
  282. this->Context.~cmListFileContext();
  283. } else {
  284. this->Bottom.~cmStateSnapshot();
  285. }
  286. }
  287. bool IsBottom() const { return !this->Parent; }
  288. union
  289. {
  290. cmStateSnapshot Bottom;
  291. cmListFileContext Context;
  292. };
  293. std::shared_ptr<Entry const> Parent;
  294. };
  295. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& snapshot)
  296. : TopEntry(std::make_shared<Entry const>(snapshot.GetCallStackBottom()))
  297. {
  298. }
  299. cmListFileBacktrace::cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  300. cmListFileContext const& lfc)
  301. : TopEntry(std::make_shared<Entry const>(std::move(parent), lfc))
  302. {
  303. }
  304. cmListFileBacktrace::cmListFileBacktrace(std::shared_ptr<Entry const> top)
  305. : TopEntry(std::move(top))
  306. {
  307. }
  308. cmStateSnapshot cmListFileBacktrace::GetBottom() const
  309. {
  310. cmStateSnapshot bottom;
  311. if (Entry const* cur = this->TopEntry.get()) {
  312. while (Entry const* parent = cur->Parent.get()) {
  313. cur = parent;
  314. }
  315. bottom = cur->Bottom;
  316. }
  317. return bottom;
  318. }
  319. cmListFileBacktrace cmListFileBacktrace::Push(std::string const& file) const
  320. {
  321. // We are entering a file-level scope but have not yet reached
  322. // any specific line or command invocation within it. This context
  323. // is useful to print when it is at the top but otherwise can be
  324. // skipped during call stack printing.
  325. cmListFileContext lfc;
  326. lfc.FilePath = file;
  327. return this->Push(lfc);
  328. }
  329. cmListFileBacktrace cmListFileBacktrace::Push(
  330. cmListFileContext const& lfc) const
  331. {
  332. assert(this->TopEntry);
  333. assert(!this->TopEntry->IsBottom() || this->TopEntry->Bottom.IsValid());
  334. return cmListFileBacktrace(this->TopEntry, lfc);
  335. }
  336. cmListFileBacktrace cmListFileBacktrace::Pop() const
  337. {
  338. assert(this->TopEntry);
  339. assert(!this->TopEntry->IsBottom());
  340. return cmListFileBacktrace(this->TopEntry->Parent);
  341. }
  342. cmListFileContext const& cmListFileBacktrace::Top() const
  343. {
  344. assert(this->TopEntry);
  345. return this->TopEntry->Context;
  346. }
  347. void cmListFileBacktrace::PrintTitle(std::ostream& out) const
  348. {
  349. // The title exists only if we have a call on top of the bottom.
  350. if (!this->TopEntry || this->TopEntry->IsBottom()) {
  351. return;
  352. }
  353. cmListFileContext lfc = this->TopEntry->Context;
  354. cmStateSnapshot bottom = this->GetBottom();
  355. cmOutputConverter converter(bottom);
  356. if (!bottom.GetState()->GetIsInTryCompile()) {
  357. lfc.FilePath = converter.ConvertToRelativePath(
  358. bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  359. }
  360. out << (lfc.Line ? " at " : " in ") << lfc;
  361. }
  362. void cmListFileBacktrace::PrintCallStack(std::ostream& out) const
  363. {
  364. // The call stack exists only if we have at least two calls on top
  365. // of the bottom.
  366. if (!this->TopEntry || this->TopEntry->IsBottom() ||
  367. this->TopEntry->Parent->IsBottom()) {
  368. return;
  369. }
  370. bool first = true;
  371. cmStateSnapshot bottom = this->GetBottom();
  372. cmOutputConverter converter(bottom);
  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 = converter.ConvertToRelativePath(
  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. }