cmListFileCache.cxx 13 KB

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