cmListFileCache.cxx 15 KB

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