cmListFileCache.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 <cassert>
  5. #include <memory>
  6. #include <sstream>
  7. #include <utility>
  8. #ifdef _WIN32
  9. # include <cmsys/Encoding.hxx>
  10. #endif
  11. #include "cmListFileLexer.h"
  12. #include "cmMessageType.h"
  13. #include "cmMessenger.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. struct cmListFileParser
  17. {
  18. cmListFileParser(cmListFile* lf, cmListFileBacktrace lfbt,
  19. cmMessenger* messenger);
  20. ~cmListFileParser();
  21. cmListFileParser(const cmListFileParser&) = delete;
  22. cmListFileParser& operator=(const cmListFileParser&) = delete;
  23. void IssueFileOpenError(std::string const& text) const;
  24. void IssueError(std::string const& text) const;
  25. bool ParseFile(const char* filename);
  26. bool ParseString(const char* str, const char* virtual_filename);
  27. bool Parse();
  28. bool ParseFunction(const char* name, long line);
  29. bool AddArgument(cmListFileLexer_Token* token,
  30. cmListFileArgument::Delimiter delim);
  31. cm::optional<cmListFileContext> CheckNesting() const;
  32. cmListFile* ListFile;
  33. cmListFileBacktrace Backtrace;
  34. cmMessenger* Messenger;
  35. const char* FileName;
  36. cmListFileLexer* Lexer;
  37. std::string FunctionName;
  38. long FunctionLine;
  39. long FunctionLineEnd;
  40. std::vector<cmListFileArgument> FunctionArguments;
  41. enum
  42. {
  43. SeparationOkay,
  44. SeparationWarning,
  45. SeparationError
  46. } Separation;
  47. };
  48. cmListFileParser::cmListFileParser(cmListFile* lf, cmListFileBacktrace lfbt,
  49. cmMessenger* messenger)
  50. : ListFile(lf)
  51. , Backtrace(std::move(lfbt))
  52. , Messenger(messenger)
  53. , FileName(nullptr)
  54. , Lexer(cmListFileLexer_New())
  55. {
  56. }
  57. cmListFileParser::~cmListFileParser()
  58. {
  59. cmListFileLexer_Delete(this->Lexer);
  60. }
  61. void cmListFileParser::IssueFileOpenError(const std::string& text) const
  62. {
  63. this->Messenger->IssueMessage(MessageType::FATAL_ERROR, text,
  64. this->Backtrace);
  65. }
  66. void cmListFileParser::IssueError(const std::string& text) const
  67. {
  68. cmListFileContext lfc;
  69. lfc.FilePath = this->FileName;
  70. lfc.Line = cmListFileLexer_GetCurrentLine(this->Lexer);
  71. cmListFileBacktrace lfbt = this->Backtrace;
  72. lfbt = lfbt.Push(lfc);
  73. this->Messenger->IssueMessage(MessageType::FATAL_ERROR, text, lfbt);
  74. cmSystemTools::SetFatalErrorOccured();
  75. }
  76. bool cmListFileParser::ParseFile(const char* filename)
  77. {
  78. this->FileName = filename;
  79. #ifdef _WIN32
  80. std::string expandedFileName = cmsys::Encoding::ToNarrow(
  81. cmSystemTools::ConvertToWindowsExtendedPath(filename));
  82. filename = expandedFileName.c_str();
  83. #endif
  84. // Open the file.
  85. cmListFileLexer_BOM bom;
  86. if (!cmListFileLexer_SetFileName(this->Lexer, filename, &bom)) {
  87. this->IssueFileOpenError("cmListFileCache: error can not open file.");
  88. return false;
  89. }
  90. if (bom == cmListFileLexer_BOM_Broken) {
  91. cmListFileLexer_SetFileName(this->Lexer, nullptr, nullptr);
  92. this->IssueFileOpenError("Error while reading Byte-Order-Mark. "
  93. "File not seekable?");
  94. return false;
  95. }
  96. // Verify the Byte-Order-Mark, if any.
  97. if (bom != cmListFileLexer_BOM_None && bom != cmListFileLexer_BOM_UTF8) {
  98. cmListFileLexer_SetFileName(this->Lexer, nullptr, nullptr);
  99. this->IssueFileOpenError(
  100. "File starts with a Byte-Order-Mark that is not UTF-8.");
  101. return false;
  102. }
  103. return this->Parse();
  104. }
  105. bool cmListFileParser::ParseString(const char* str,
  106. const char* virtual_filename)
  107. {
  108. this->FileName = virtual_filename;
  109. if (!cmListFileLexer_SetString(this->Lexer, str)) {
  110. this->IssueFileOpenError("cmListFileCache: cannot allocate buffer.");
  111. return false;
  112. }
  113. return this->Parse();
  114. }
  115. bool cmListFileParser::Parse()
  116. {
  117. // Use a simple recursive-descent parser to process the token
  118. // stream.
  119. bool haveNewline = true;
  120. while (cmListFileLexer_Token* token = cmListFileLexer_Scan(this->Lexer)) {
  121. if (token->type == cmListFileLexer_Token_Space) {
  122. } else if (token->type == cmListFileLexer_Token_Newline) {
  123. haveNewline = true;
  124. } else if (token->type == cmListFileLexer_Token_CommentBracket) {
  125. haveNewline = false;
  126. } else if (token->type == cmListFileLexer_Token_Identifier) {
  127. if (haveNewline) {
  128. haveNewline = false;
  129. if (this->ParseFunction(token->text, token->line)) {
  130. this->ListFile->Functions.emplace_back(
  131. std::move(this->FunctionName), this->FunctionLine,
  132. this->FunctionLineEnd, std::move(this->FunctionArguments));
  133. } else {
  134. return false;
  135. }
  136. } else {
  137. std::ostringstream error;
  138. error << "Parse error. Expected a newline, got "
  139. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  140. << " with text \"" << token->text << "\".";
  141. this->IssueError(error.str());
  142. return false;
  143. }
  144. } else {
  145. std::ostringstream error;
  146. error << "Parse error. Expected a command name, got "
  147. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  148. << " with text \"" << token->text << "\".";
  149. this->IssueError(error.str());
  150. return false;
  151. }
  152. }
  153. // Check if all functions are nested properly.
  154. if (auto badNesting = this->CheckNesting()) {
  155. this->Messenger->IssueMessage(
  156. MessageType::FATAL_ERROR,
  157. "Flow control statements are not properly nested.",
  158. this->Backtrace.Push(*badNesting));
  159. cmSystemTools::SetFatalErrorOccured();
  160. return false;
  161. }
  162. return true;
  163. }
  164. bool cmListFile::ParseFile(const char* filename, cmMessenger* messenger,
  165. cmListFileBacktrace const& lfbt)
  166. {
  167. if (!cmSystemTools::FileExists(filename) ||
  168. cmSystemTools::FileIsDirectory(filename)) {
  169. return false;
  170. }
  171. bool parseError = false;
  172. {
  173. cmListFileParser parser(this, lfbt, messenger);
  174. parseError = !parser.ParseFile(filename);
  175. }
  176. return !parseError;
  177. }
  178. bool cmListFile::ParseString(const char* str, const char* virtual_filename,
  179. cmMessenger* messenger,
  180. const cmListFileBacktrace& lfbt)
  181. {
  182. bool parseError = false;
  183. {
  184. cmListFileParser parser(this, lfbt, messenger);
  185. parseError = !parser.ParseString(str, virtual_filename);
  186. }
  187. return !parseError;
  188. }
  189. bool cmListFileParser::ParseFunction(const char* name, long line)
  190. {
  191. // Ininitialize a new function call.
  192. this->FunctionName = name;
  193. this->FunctionLine = line;
  194. // Command name has already been parsed. Read the left paren.
  195. cmListFileLexer_Token* token;
  196. while ((token = cmListFileLexer_Scan(this->Lexer)) &&
  197. token->type == cmListFileLexer_Token_Space) {
  198. }
  199. if (!token) {
  200. std::ostringstream error;
  201. /* clang-format off */
  202. error << "Unexpected end of file.\n"
  203. << "Parse error. Function missing opening \"(\".";
  204. /* clang-format on */
  205. this->IssueError(error.str());
  206. return false;
  207. }
  208. if (token->type != cmListFileLexer_Token_ParenLeft) {
  209. std::ostringstream error;
  210. error << "Parse error. Expected \"(\", got "
  211. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  212. << " with text \"" << token->text << "\".";
  213. this->IssueError(error.str());
  214. return false;
  215. }
  216. // Arguments.
  217. unsigned long parenDepth = 0;
  218. this->Separation = SeparationOkay;
  219. while ((token = cmListFileLexer_Scan(this->Lexer))) {
  220. if (token->type == cmListFileLexer_Token_Space ||
  221. token->type == cmListFileLexer_Token_Newline) {
  222. this->Separation = SeparationOkay;
  223. continue;
  224. }
  225. if (token->type == cmListFileLexer_Token_ParenLeft) {
  226. parenDepth++;
  227. this->Separation = SeparationOkay;
  228. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  229. return false;
  230. }
  231. } else if (token->type == cmListFileLexer_Token_ParenRight) {
  232. if (parenDepth == 0) {
  233. this->FunctionLineEnd = token->line;
  234. return true;
  235. }
  236. parenDepth--;
  237. this->Separation = SeparationOkay;
  238. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  239. return false;
  240. }
  241. this->Separation = SeparationWarning;
  242. } else if (token->type == cmListFileLexer_Token_Identifier ||
  243. token->type == cmListFileLexer_Token_ArgumentUnquoted) {
  244. if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
  245. return false;
  246. }
  247. this->Separation = SeparationWarning;
  248. } else if (token->type == cmListFileLexer_Token_ArgumentQuoted) {
  249. if (!this->AddArgument(token, cmListFileArgument::Quoted)) {
  250. return false;
  251. }
  252. this->Separation = SeparationWarning;
  253. } else if (token->type == cmListFileLexer_Token_ArgumentBracket) {
  254. if (!this->AddArgument(token, cmListFileArgument::Bracket)) {
  255. return false;
  256. }
  257. this->Separation = SeparationError;
  258. } else if (token->type == cmListFileLexer_Token_CommentBracket) {
  259. this->Separation = SeparationError;
  260. } else {
  261. // Error.
  262. std::ostringstream error;
  263. error << "Parse error. Function missing ending \")\". "
  264. << "Instead found "
  265. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  266. << " with text \"" << token->text << "\".";
  267. this->IssueError(error.str());
  268. return false;
  269. }
  270. }
  271. std::ostringstream error;
  272. cmListFileContext lfc;
  273. lfc.FilePath = this->FileName;
  274. lfc.Line = line;
  275. cmListFileBacktrace lfbt = this->Backtrace;
  276. lfbt = lfbt.Push(lfc);
  277. error << "Parse error. Function missing ending \")\". "
  278. << "End of file reached.";
  279. this->Messenger->IssueMessage(MessageType::FATAL_ERROR, error.str(), lfbt);
  280. return false;
  281. }
  282. bool cmListFileParser::AddArgument(cmListFileLexer_Token* token,
  283. cmListFileArgument::Delimiter delim)
  284. {
  285. this->FunctionArguments.emplace_back(token->text, delim, token->line);
  286. if (this->Separation == SeparationOkay) {
  287. return true;
  288. }
  289. bool isError = (this->Separation == SeparationError ||
  290. delim == cmListFileArgument::Bracket);
  291. std::ostringstream m;
  292. cmListFileContext lfc;
  293. lfc.FilePath = this->FileName;
  294. lfc.Line = token->line;
  295. cmListFileBacktrace lfbt = this->Backtrace;
  296. lfbt = lfbt.Push(lfc);
  297. m << "Syntax " << (isError ? "Error" : "Warning") << " in cmake code at "
  298. << "column " << token->column << "\n"
  299. << "Argument not separated from preceding token by whitespace.";
  300. /* clang-format on */
  301. if (isError) {
  302. this->Messenger->IssueMessage(MessageType::FATAL_ERROR, m.str(), lfbt);
  303. return false;
  304. }
  305. this->Messenger->IssueMessage(MessageType::AUTHOR_WARNING, m.str(), lfbt);
  306. return true;
  307. }
  308. namespace {
  309. enum class NestingStateEnum
  310. {
  311. If,
  312. Else,
  313. While,
  314. Foreach,
  315. Function,
  316. Macro,
  317. };
  318. struct NestingState
  319. {
  320. NestingStateEnum State;
  321. cmListFileContext Context;
  322. };
  323. bool TopIs(std::vector<NestingState>& stack, NestingStateEnum state)
  324. {
  325. return !stack.empty() && stack.back().State == state;
  326. }
  327. }
  328. cm::optional<cmListFileContext> cmListFileParser::CheckNesting() const
  329. {
  330. std::vector<NestingState> stack;
  331. for (auto const& func : this->ListFile->Functions) {
  332. auto const& name = func.LowerCaseName();
  333. if (name == "if") {
  334. stack.push_back({
  335. NestingStateEnum::If,
  336. cmListFileContext::FromListFileFunction(func, this->FileName),
  337. });
  338. } else if (name == "elseif") {
  339. if (!TopIs(stack, NestingStateEnum::If)) {
  340. return cmListFileContext::FromListFileFunction(func, this->FileName);
  341. }
  342. stack.back() = {
  343. NestingStateEnum::If,
  344. cmListFileContext::FromListFileFunction(func, this->FileName),
  345. };
  346. } else if (name == "else") {
  347. if (!TopIs(stack, NestingStateEnum::If)) {
  348. return cmListFileContext::FromListFileFunction(func, this->FileName);
  349. }
  350. stack.back() = {
  351. NestingStateEnum::Else,
  352. cmListFileContext::FromListFileFunction(func, this->FileName),
  353. };
  354. } else if (name == "endif") {
  355. if (!TopIs(stack, NestingStateEnum::If) &&
  356. !TopIs(stack, NestingStateEnum::Else)) {
  357. return cmListFileContext::FromListFileFunction(func, this->FileName);
  358. }
  359. stack.pop_back();
  360. } else if (name == "while") {
  361. stack.push_back({
  362. NestingStateEnum::While,
  363. cmListFileContext::FromListFileFunction(func, this->FileName),
  364. });
  365. } else if (name == "endwhile") {
  366. if (!TopIs(stack, NestingStateEnum::While)) {
  367. return cmListFileContext::FromListFileFunction(func, this->FileName);
  368. }
  369. stack.pop_back();
  370. } else if (name == "foreach") {
  371. stack.push_back({
  372. NestingStateEnum::Foreach,
  373. cmListFileContext::FromListFileFunction(func, this->FileName),
  374. });
  375. } else if (name == "endforeach") {
  376. if (!TopIs(stack, NestingStateEnum::Foreach)) {
  377. return cmListFileContext::FromListFileFunction(func, this->FileName);
  378. }
  379. stack.pop_back();
  380. } else if (name == "function") {
  381. stack.push_back({
  382. NestingStateEnum::Function,
  383. cmListFileContext::FromListFileFunction(func, this->FileName),
  384. });
  385. } else if (name == "endfunction") {
  386. if (!TopIs(stack, NestingStateEnum::Function)) {
  387. return cmListFileContext::FromListFileFunction(func, this->FileName);
  388. }
  389. stack.pop_back();
  390. } else if (name == "macro") {
  391. stack.push_back({
  392. NestingStateEnum::Macro,
  393. cmListFileContext::FromListFileFunction(func, this->FileName),
  394. });
  395. } else if (name == "endmacro") {
  396. if (!TopIs(stack, NestingStateEnum::Macro)) {
  397. return cmListFileContext::FromListFileFunction(func, this->FileName);
  398. }
  399. stack.pop_back();
  400. }
  401. }
  402. if (!stack.empty()) {
  403. return stack.back().Context;
  404. }
  405. return cm::nullopt;
  406. }
  407. // We hold a call/file context.
  408. struct cmListFileBacktrace::Entry
  409. {
  410. Entry(std::shared_ptr<Entry const> parent, cmListFileContext lfc)
  411. : Context(std::move(lfc))
  412. , Parent(std::move(parent))
  413. {
  414. }
  415. cmListFileContext Context;
  416. std::shared_ptr<Entry const> Parent;
  417. };
  418. /* NOLINTNEXTLINE(performance-unnecessary-value-param) */
  419. cmListFileBacktrace::cmListFileBacktrace(std::shared_ptr<Entry const> parent,
  420. cmListFileContext const& lfc)
  421. : TopEntry(std::make_shared<Entry const>(std::move(parent), lfc))
  422. {
  423. }
  424. cmListFileBacktrace::cmListFileBacktrace(std::shared_ptr<Entry const> top)
  425. : TopEntry(std::move(top))
  426. {
  427. }
  428. cmListFileBacktrace cmListFileBacktrace::Push(std::string const& file) const
  429. {
  430. // We are entering a file-level scope but have not yet reached
  431. // any specific line or command invocation within it. This context
  432. // is useful to print when it is at the top but otherwise can be
  433. // skipped during call stack printing.
  434. cmListFileContext lfc;
  435. lfc.FilePath = file;
  436. return this->Push(lfc);
  437. }
  438. cmListFileBacktrace cmListFileBacktrace::Push(
  439. cmListFileContext const& lfc) const
  440. {
  441. return cmListFileBacktrace(this->TopEntry, lfc);
  442. }
  443. cmListFileBacktrace cmListFileBacktrace::Pop() const
  444. {
  445. assert(this->TopEntry);
  446. return cmListFileBacktrace(this->TopEntry->Parent);
  447. }
  448. cmListFileContext const& cmListFileBacktrace::Top() const
  449. {
  450. assert(this->TopEntry);
  451. return this->TopEntry->Context;
  452. }
  453. bool cmListFileBacktrace::Empty() const
  454. {
  455. return !this->TopEntry;
  456. }
  457. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  458. {
  459. os << lfc.FilePath;
  460. if (lfc.Line > 0) {
  461. os << ":" << lfc.Line;
  462. if (!lfc.Name.empty()) {
  463. os << " (" << lfc.Name << ")";
  464. }
  465. } else if (lfc.Line == cmListFileContext::DeferPlaceholderLine) {
  466. os << ":DEFERRED";
  467. }
  468. return os;
  469. }
  470. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs)
  471. {
  472. if (lhs.Line != rhs.Line) {
  473. return lhs.Line < rhs.Line;
  474. }
  475. return lhs.FilePath < rhs.FilePath;
  476. }
  477. bool operator==(const cmListFileContext& lhs, const cmListFileContext& rhs)
  478. {
  479. return lhs.Line == rhs.Line && lhs.FilePath == rhs.FilePath;
  480. }
  481. bool operator!=(const cmListFileContext& lhs, const cmListFileContext& rhs)
  482. {
  483. return !(lhs == rhs);
  484. }
  485. std::ostream& operator<<(std::ostream& os, BT<std::string> const& s)
  486. {
  487. return os << s.Value;
  488. }
  489. std::vector<BT<std::string>> cmExpandListWithBacktrace(
  490. std::string const& list, cmListFileBacktrace const& bt, bool emptyArgs)
  491. {
  492. std::vector<BT<std::string>> result;
  493. std::vector<std::string> tmp = cmExpandedList(list, emptyArgs);
  494. result.reserve(tmp.size());
  495. for (std::string& i : tmp) {
  496. result.emplace_back(std::move(i), bt);
  497. }
  498. return result;
  499. }