cmListFileCache.cxx 13 KB

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