cmListFileCache.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 const& bottom,
  288. Entry* up,
  289. cmListFileContext const& lfc)
  290. : Bottom(bottom)
  291. , Cur(new Entry(lfc, up))
  292. {
  293. assert(this->Bottom.IsValid());
  294. this->Cur->Ref();
  295. }
  296. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& bottom,
  297. Entry* cur)
  298. : Bottom(bottom)
  299. , Cur(cur)
  300. {
  301. if (this->Cur) {
  302. assert(this->Bottom.IsValid());
  303. this->Cur->Ref();
  304. }
  305. }
  306. cmListFileBacktrace::cmListFileBacktrace()
  307. : Bottom()
  308. , Cur(CM_NULLPTR)
  309. {
  310. }
  311. cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& snapshot)
  312. : Bottom(snapshot.GetCallStackBottom())
  313. , Cur(CM_NULLPTR)
  314. {
  315. }
  316. cmListFileBacktrace::cmListFileBacktrace(cmListFileBacktrace const& r)
  317. : Bottom(r.Bottom)
  318. , Cur(r.Cur)
  319. {
  320. if (this->Cur) {
  321. assert(this->Bottom.IsValid());
  322. this->Cur->Ref();
  323. }
  324. }
  325. cmListFileBacktrace& cmListFileBacktrace::operator=(
  326. cmListFileBacktrace const& r)
  327. {
  328. cmListFileBacktrace tmp(r);
  329. std::swap(this->Cur, tmp.Cur);
  330. std::swap(this->Bottom, tmp.Bottom);
  331. return *this;
  332. }
  333. cmListFileBacktrace::~cmListFileBacktrace()
  334. {
  335. if (this->Cur) {
  336. this->Cur->Unref();
  337. }
  338. }
  339. cmListFileBacktrace cmListFileBacktrace::Push(std::string const& file) const
  340. {
  341. // We are entering a file-level scope but have not yet reached
  342. // any specific line or command invocation within it. This context
  343. // is useful to print when it is at the top but otherwise can be
  344. // skipped during call stack printing.
  345. cmListFileContext lfc;
  346. lfc.FilePath = file;
  347. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  348. }
  349. cmListFileBacktrace cmListFileBacktrace::Push(
  350. cmListFileContext const& lfc) const
  351. {
  352. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  353. }
  354. cmListFileBacktrace cmListFileBacktrace::Pop() const
  355. {
  356. assert(this->Cur);
  357. return cmListFileBacktrace(this->Bottom, this->Cur->Up);
  358. }
  359. cmListFileContext const& cmListFileBacktrace::Top() const
  360. {
  361. if (this->Cur) {
  362. return *this->Cur;
  363. }
  364. static cmListFileContext const empty;
  365. return empty;
  366. }
  367. void cmListFileBacktrace::PrintTitle(std::ostream& out) const
  368. {
  369. if (!this->Cur) {
  370. return;
  371. }
  372. cmOutputConverter converter(this->Bottom);
  373. cmListFileContext lfc = *this->Cur;
  374. if (!this->Bottom.GetState()->GetIsInTryCompile()) {
  375. lfc.FilePath = converter.ConvertToRelativePath(
  376. this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  377. }
  378. out << (lfc.Line ? " at " : " in ") << lfc;
  379. }
  380. void cmListFileBacktrace::PrintCallStack(std::ostream& out) const
  381. {
  382. if (!this->Cur || !this->Cur->Up) {
  383. return;
  384. }
  385. bool first = true;
  386. cmOutputConverter converter(this->Bottom);
  387. for (Entry* i = this->Cur->Up; i; i = i->Up) {
  388. if (i->Name.empty()) {
  389. // Skip this whole-file scope. When we get here we already will
  390. // have printed a more-specific context within the file.
  391. continue;
  392. }
  393. if (first) {
  394. first = false;
  395. out << "Call Stack (most recent call first):\n";
  396. }
  397. cmListFileContext lfc = *i;
  398. if (!this->Bottom.GetState()->GetIsInTryCompile()) {
  399. lfc.FilePath = converter.ConvertToRelativePath(
  400. this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
  401. }
  402. out << " " << lfc << "\n";
  403. }
  404. }
  405. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  406. {
  407. os << lfc.FilePath;
  408. if (lfc.Line) {
  409. os << ":" << lfc.Line;
  410. if (!lfc.Name.empty()) {
  411. os << " (" << lfc.Name << ")";
  412. }
  413. }
  414. return os;
  415. }
  416. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs)
  417. {
  418. if (lhs.Line != rhs.Line) {
  419. return lhs.Line < rhs.Line;
  420. }
  421. return lhs.FilePath < rhs.FilePath;
  422. }
  423. bool operator==(const cmListFileContext& lhs, const cmListFileContext& rhs)
  424. {
  425. return lhs.Line == rhs.Line && lhs.FilePath == rhs.FilePath;
  426. }
  427. bool operator!=(const cmListFileContext& lhs, const cmListFileContext& rhs)
  428. {
  429. return !(lhs == rhs);
  430. }