cmListFileCache.cxx 15 KB

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