cmListFileCache.cxx 13 KB

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