cmListFileCache.cxx 15 KB

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