cmListFileCache.cxx 13 KB

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