cmake/Source/cmListFileCache.cxx

510 lines
15 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2022-08-04 22:12:04 +02:00
#define cmListFileCache_cxx
#include "cmListFileCache.h"
2020-02-01 23:06:01 +01:00
#include <memory>
#include <sstream>
#include <utility>
2021-09-14 00:13:48 +02:00
#ifdef _WIN32
# include <cmsys/Encoding.hxx>
#endif
2023-07-02 19:51:09 +02:00
#include "cmList.h"
#include "cmListFileLexer.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2016-10-30 18:24:19 +01:00
#include "cmMessenger.h"
#include "cmSystemTools.h"
2013-11-03 12:27:13 +02:00
struct cmListFileParser
{
2019-11-11 23:01:05 +01:00
cmListFileParser(cmListFile* lf, cmListFileBacktrace lfbt,
2020-08-30 11:54:41 +02:00
cmMessenger* messenger);
2013-11-03 12:27:13 +02:00
~cmListFileParser();
2019-11-11 23:01:05 +01:00
cmListFileParser(const cmListFileParser&) = delete;
cmListFileParser& operator=(const cmListFileParser&) = delete;
2016-10-30 18:24:19 +01:00
void IssueFileOpenError(std::string const& text) const;
void IssueError(std::string const& text) const;
2020-08-30 11:54:41 +02:00
bool ParseFile(const char* filename);
bool ParseString(const char* str, const char* virtual_filename);
bool Parse();
2013-11-03 12:27:13 +02:00
bool ParseFunction(const char* name, long line);
2014-08-03 19:52:23 +02:00
bool AddArgument(cmListFileLexer_Token* token,
2013-11-03 12:27:13 +02:00
cmListFileArgument::Delimiter delim);
2021-09-14 00:13:48 +02:00
cm::optional<cmListFileContext> CheckNesting() const;
2013-11-03 12:27:13 +02:00
cmListFile* ListFile;
2016-10-30 18:24:19 +01:00
cmListFileBacktrace Backtrace;
cmMessenger* Messenger;
2022-08-04 22:12:04 +02:00
const char* FileName = nullptr;
2013-11-03 12:27:13 +02:00
cmListFileLexer* Lexer;
2021-09-14 00:13:48 +02:00
std::string FunctionName;
long FunctionLine;
2022-08-04 22:12:04 +02:00
long FunctionLineEnd;
2021-09-14 00:13:48 +02:00
std::vector<cmListFileArgument> FunctionArguments;
2016-07-09 11:21:54 +02:00
enum
{
SeparationOkay,
SeparationWarning,
SeparationError
} Separation;
2013-11-03 12:27:13 +02:00
};
2019-11-11 23:01:05 +01:00
cmListFileParser::cmListFileParser(cmListFile* lf, cmListFileBacktrace lfbt,
2020-08-30 11:54:41 +02:00
cmMessenger* messenger)
2016-07-09 11:21:54 +02:00
: ListFile(lf)
2019-11-11 23:01:05 +01:00
, Backtrace(std::move(lfbt))
2016-10-30 18:24:19 +01:00
, Messenger(messenger)
2016-07-09 11:21:54 +02:00
, Lexer(cmListFileLexer_New())
{
2013-11-03 12:27:13 +02:00
}
2013-11-03 12:27:13 +02:00
cmListFileParser::~cmListFileParser()
{
cmListFileLexer_Delete(this->Lexer);
}
2016-10-30 18:24:19 +01:00
void cmListFileParser::IssueFileOpenError(const std::string& text) const
{
2019-11-11 23:01:05 +01:00
this->Messenger->IssueMessage(MessageType::FATAL_ERROR, text,
this->Backtrace);
2016-10-30 18:24:19 +01:00
}
void cmListFileParser::IssueError(const std::string& text) const
{
cmListFileContext lfc;
lfc.FilePath = this->FileName;
lfc.Line = cmListFileLexer_GetCurrentLine(this->Lexer);
cmListFileBacktrace lfbt = this->Backtrace;
lfbt = lfbt.Push(lfc);
2019-11-11 23:01:05 +01:00
this->Messenger->IssueMessage(MessageType::FATAL_ERROR, text, lfbt);
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2016-10-30 18:24:19 +01:00
}
2020-08-30 11:54:41 +02:00
bool cmListFileParser::ParseFile(const char* filename)
2013-11-03 12:27:13 +02:00
{
2020-08-30 11:54:41 +02:00
this->FileName = filename;
2021-09-14 00:13:48 +02:00
#ifdef _WIN32
std::string expandedFileName = cmsys::Encoding::ToNarrow(
cmSystemTools::ConvertToWindowsExtendedPath(filename));
filename = expandedFileName.c_str();
#endif
// Open the file.
2014-08-03 19:52:23 +02:00
cmListFileLexer_BOM bom;
2021-09-14 00:13:48 +02:00
if (!cmListFileLexer_SetFileName(this->Lexer, filename, &bom)) {
2016-10-30 18:24:19 +01:00
this->IssueFileOpenError("cmListFileCache: error can not open file.");
return false;
2016-07-09 11:21:54 +02:00
}
2017-07-20 19:35:53 +02:00
if (bom == cmListFileLexer_BOM_Broken) {
2018-01-26 17:06:56 +01:00
cmListFileLexer_SetFileName(this->Lexer, nullptr, nullptr);
2017-07-20 19:35:53 +02:00
this->IssueFileOpenError("Error while reading Byte-Order-Mark. "
"File not seekable?");
return false;
}
2014-08-03 19:52:23 +02:00
// Verify the Byte-Order-Mark, if any.
2016-07-09 11:21:54 +02:00
if (bom != cmListFileLexer_BOM_None && bom != cmListFileLexer_BOM_UTF8) {
2018-01-26 17:06:56 +01:00
cmListFileLexer_SetFileName(this->Lexer, nullptr, nullptr);
2016-10-30 18:24:19 +01:00
this->IssueFileOpenError(
"File starts with a Byte-Order-Mark that is not UTF-8.");
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2021-09-14 00:13:48 +02:00
return this->Parse();
2020-08-30 11:54:41 +02:00
}
bool cmListFileParser::ParseString(const char* str,
const char* virtual_filename)
{
this->FileName = virtual_filename;
if (!cmListFileLexer_SetString(this->Lexer, str)) {
this->IssueFileOpenError("cmListFileCache: cannot allocate buffer.");
return false;
}
2021-09-14 00:13:48 +02:00
return this->Parse();
2020-08-30 11:54:41 +02:00
}
bool cmListFileParser::Parse()
{
// Use a simple recursive-descent parser to process the token
// stream.
bool haveNewline = true;
2016-07-09 11:21:54 +02:00
while (cmListFileLexer_Token* token = cmListFileLexer_Scan(this->Lexer)) {
if (token->type == cmListFileLexer_Token_Space) {
} else if (token->type == cmListFileLexer_Token_Newline) {
haveNewline = true;
2016-07-09 11:21:54 +02:00
} else if (token->type == cmListFileLexer_Token_CommentBracket) {
2014-08-03 19:52:23 +02:00
haveNewline = false;
2016-07-09 11:21:54 +02:00
} else if (token->type == cmListFileLexer_Token_Identifier) {
if (haveNewline) {
haveNewline = false;
2016-07-09 11:21:54 +02:00
if (this->ParseFunction(token->text, token->line)) {
2021-09-14 00:13:48 +02:00
this->ListFile->Functions.emplace_back(
std::move(this->FunctionName), this->FunctionLine,
2022-08-04 22:12:04 +02:00
this->FunctionLineEnd, std::move(this->FunctionArguments));
2016-07-09 11:21:54 +02:00
} else {
2013-11-03 12:27:13 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
std::ostringstream error;
2016-10-30 18:24:19 +01:00
error << "Parse error. Expected a newline, got "
2013-11-03 12:27:13 +02:00
<< cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
<< " with text \"" << token->text << "\".";
2016-10-30 18:24:19 +01:00
this->IssueError(error.str());
2013-11-03 12:27:13 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
std::ostringstream error;
2016-10-30 18:24:19 +01:00
error << "Parse error. Expected a command name, got "
2013-11-03 12:27:13 +02:00
<< cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
2016-07-09 11:21:54 +02:00
<< " with text \"" << token->text << "\".";
2016-10-30 18:24:19 +01:00
this->IssueError(error.str());
2013-11-03 12:27:13 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
// Check if all functions are nested properly.
if (auto badNesting = this->CheckNesting()) {
this->Messenger->IssueMessage(
MessageType::FATAL_ERROR,
"Flow control statements are not properly nested.",
this->Backtrace.Push(*badNesting));
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2021-09-14 00:13:48 +02:00
return false;
}
2013-11-03 12:27:13 +02:00
return true;
}
2016-10-30 18:24:19 +01:00
bool cmListFile::ParseFile(const char* filename, cmMessenger* messenger,
cmListFileBacktrace const& lfbt)
2013-11-03 12:27:13 +02:00
{
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::FileExists(filename) ||
cmSystemTools::FileIsDirectory(filename)) {
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
bool parseError = false;
{
2020-08-30 11:54:41 +02:00
cmListFileParser parser(this, lfbt, messenger);
parseError = !parser.ParseFile(filename);
}
return !parseError;
}
bool cmListFile::ParseString(const char* str, const char* virtual_filename,
cmMessenger* messenger,
const cmListFileBacktrace& lfbt)
{
bool parseError = false;
{
cmListFileParser parser(this, lfbt, messenger);
parseError = !parser.ParseString(str, virtual_filename);
2013-11-03 12:27:13 +02:00
}
2016-10-30 18:24:19 +01:00
return !parseError;
}
2013-11-03 12:27:13 +02:00
bool cmListFileParser::ParseFunction(const char* name, long line)
{
2016-07-09 11:21:54 +02:00
// Ininitialize a new function call.
2021-09-14 00:13:48 +02:00
this->FunctionName = name;
this->FunctionLine = line;
2013-11-03 12:27:13 +02:00
// Command name has already been parsed. Read the left paren.
cmListFileLexer_Token* token;
2016-07-09 11:21:54 +02:00
while ((token = cmListFileLexer_Scan(this->Lexer)) &&
token->type == cmListFileLexer_Token_Space) {
}
if (!token) {
2015-04-27 22:25:09 +02:00
std::ostringstream error;
2016-07-09 11:21:54 +02:00
/* clang-format off */
2016-10-30 18:24:19 +01:00
error << "Unexpected end of file.\n"
<< "Parse error. Function missing opening \"(\".";
2016-07-09 11:21:54 +02:00
/* clang-format on */
2016-10-30 18:24:19 +01:00
this->IssueError(error.str());
return false;
2016-07-09 11:21:54 +02:00
}
if (token->type != cmListFileLexer_Token_ParenLeft) {
2015-04-27 22:25:09 +02:00
std::ostringstream error;
2016-10-30 18:24:19 +01:00
error << "Parse error. Expected \"(\", got "
2013-11-03 12:27:13 +02:00
<< cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
<< " with text \"" << token->text << "\".";
2016-10-30 18:24:19 +01:00
this->IssueError(error.str());
return false;
2016-07-09 11:21:54 +02:00
}
// Arguments.
unsigned long parenDepth = 0;
2013-11-03 12:27:13 +02:00
this->Separation = SeparationOkay;
2019-11-11 23:01:05 +01:00
while ((token = cmListFileLexer_Scan(this->Lexer))) {
2016-07-09 11:21:54 +02:00
if (token->type == cmListFileLexer_Token_Space ||
token->type == cmListFileLexer_Token_Newline) {
2013-11-03 12:27:13 +02:00
this->Separation = SeparationOkay;
continue;
2016-07-09 11:21:54 +02:00
}
if (token->type == cmListFileLexer_Token_ParenLeft) {
parenDepth++;
2013-11-03 12:27:13 +02:00
this->Separation = SeparationOkay;
2016-07-09 11:21:54 +02:00
if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
2014-08-03 19:52:23 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
} else if (token->type == cmListFileLexer_Token_ParenRight) {
if (parenDepth == 0) {
2022-08-04 22:12:04 +02:00
this->FunctionLineEnd = token->line;
return true;
2016-07-09 11:21:54 +02:00
}
parenDepth--;
2013-11-03 12:27:13 +02:00
this->Separation = SeparationOkay;
2016-07-09 11:21:54 +02:00
if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
2014-08-03 19:52:23 +02:00
return false;
}
2013-11-03 12:27:13 +02:00
this->Separation = SeparationWarning;
2016-07-09 11:21:54 +02:00
} else if (token->type == cmListFileLexer_Token_Identifier ||
token->type == cmListFileLexer_Token_ArgumentUnquoted) {
if (!this->AddArgument(token, cmListFileArgument::Unquoted)) {
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
this->Separation = SeparationWarning;
2016-07-09 11:21:54 +02:00
} else if (token->type == cmListFileLexer_Token_ArgumentQuoted) {
if (!this->AddArgument(token, cmListFileArgument::Quoted)) {
return false;
}
2016-07-09 11:21:54 +02:00
this->Separation = SeparationWarning;
} else if (token->type == cmListFileLexer_Token_ArgumentBracket) {
if (!this->AddArgument(token, cmListFileArgument::Bracket)) {
2014-08-03 19:52:23 +02:00
return false;
}
this->Separation = SeparationError;
2016-07-09 11:21:54 +02:00
} else if (token->type == cmListFileLexer_Token_CommentBracket) {
this->Separation = SeparationError;
} else {
// Error.
2015-04-27 22:25:09 +02:00
std::ostringstream error;
2016-10-30 18:24:19 +01:00
error << "Parse error. Function missing ending \")\". "
<< "Instead found "
2013-11-03 12:27:13 +02:00
<< cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
<< " with text \"" << token->text << "\".";
2016-10-30 18:24:19 +01:00
this->IssueError(error.str());
return false;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::ostringstream error;
2016-10-30 18:24:19 +01:00
cmListFileContext lfc;
lfc.FilePath = this->FileName;
2019-11-11 23:01:05 +01:00
lfc.Line = line;
2016-10-30 18:24:19 +01:00
cmListFileBacktrace lfbt = this->Backtrace;
lfbt = lfbt.Push(lfc);
error << "Parse error. Function missing ending \")\". "
<< "End of file reached.";
2019-11-11 23:01:05 +01:00
this->Messenger->IssueMessage(MessageType::FATAL_ERROR, error.str(), lfbt);
return false;
}
2014-08-03 19:52:23 +02:00
bool cmListFileParser::AddArgument(cmListFileLexer_Token* token,
2013-11-03 12:27:13 +02:00
cmListFileArgument::Delimiter delim)
{
2021-09-14 00:13:48 +02:00
this->FunctionArguments.emplace_back(token->text, delim, token->line);
2016-07-09 11:21:54 +02:00
if (this->Separation == SeparationOkay) {
2014-08-03 19:52:23 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
bool isError = (this->Separation == SeparationError ||
delim == cmListFileArgument::Bracket);
2015-04-27 22:25:09 +02:00
std::ostringstream m;
2016-10-30 18:24:19 +01:00
cmListFileContext lfc;
lfc.FilePath = this->FileName;
lfc.Line = token->line;
cmListFileBacktrace lfbt = this->Backtrace;
lfbt = lfbt.Push(lfc);
m << "Syntax " << (isError ? "Error" : "Warning") << " in cmake code at "
<< "column " << token->column << "\n"
2013-11-03 12:27:13 +02:00
<< "Argument not separated from preceding token by whitespace.";
2016-07-09 11:21:54 +02:00
/* clang-format on */
if (isError) {
2019-11-11 23:01:05 +01:00
this->Messenger->IssueMessage(MessageType::FATAL_ERROR, m.str(), lfbt);
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
this->Messenger->IssueMessage(MessageType::AUTHOR_WARNING, m.str(), lfbt);
2016-10-30 18:24:19 +01:00
return true;
2013-11-03 12:27:13 +02:00
}
2021-09-14 00:13:48 +02:00
namespace {
enum class NestingStateEnum
{
If,
Else,
While,
Foreach,
Function,
Macro,
2022-11-16 20:14:03 +01:00
Block
2021-09-14 00:13:48 +02:00
};
struct NestingState
{
NestingStateEnum State;
cmListFileContext Context;
};
bool TopIs(std::vector<NestingState>& stack, NestingStateEnum state)
{
return !stack.empty() && stack.back().State == state;
}
}
cm::optional<cmListFileContext> cmListFileParser::CheckNesting() const
{
std::vector<NestingState> stack;
for (auto const& func : this->ListFile->Functions) {
auto const& name = func.LowerCaseName();
if (name == "if") {
stack.push_back({
NestingStateEnum::If,
2022-03-29 21:10:50 +02:00
cmListFileContext::FromListFileFunction(func, this->FileName),
2021-09-14 00:13:48 +02:00
});
} else if (name == "elseif") {
if (!TopIs(stack, NestingStateEnum::If)) {
2022-03-29 21:10:50 +02:00
return cmListFileContext::FromListFileFunction(func, this->FileName);
2021-09-14 00:13:48 +02:00
}
stack.back() = {
NestingStateEnum::If,
2022-03-29 21:10:50 +02:00
cmListFileContext::FromListFileFunction(func, this->FileName),
2021-09-14 00:13:48 +02:00
};
} else if (name == "else") {
if (!TopIs(stack, NestingStateEnum::If)) {
2022-03-29 21:10:50 +02:00
return cmListFileContext::FromListFileFunction(func, this->FileName);
2021-09-14 00:13:48 +02:00
}
stack.back() = {
NestingStateEnum::Else,
2022-03-29 21:10:50 +02:00
cmListFileContext::FromListFileFunction(func, this->FileName),
2021-09-14 00:13:48 +02:00
};
} else if (name == "endif") {
if (!TopIs(stack, NestingStateEnum::If) &&
!TopIs(stack, NestingStateEnum::Else)) {
2022-03-29 21:10:50 +02:00
return cmListFileContext::FromListFileFunction(func, this->FileName);
2021-09-14 00:13:48 +02:00
}
stack.pop_back();
} else if (name == "while") {
stack.push_back({
NestingStateEnum::While,
2022-03-29 21:10:50 +02:00
cmListFileContext::FromListFileFunction(func, this->FileName),
2021-09-14 00:13:48 +02:00
});
} else if (name == "endwhile") {
if (!TopIs(stack, NestingStateEnum::While)) {
2022-03-29 21:10:50 +02:00
return cmListFileContext::FromListFileFunction(func, this->FileName);
2021-09-14 00:13:48 +02:00
}
stack.pop_back();
} else if (name == "foreach") {
stack.push_back({
NestingStateEnum::Foreach,
2022-03-29 21:10:50 +02:00
cmListFileContext::FromListFileFunction(func, this->FileName),
2021-09-14 00:13:48 +02:00
});
} else if (name == "endforeach") {
if (!TopIs(stack, NestingStateEnum::Foreach)) {
2022-03-29 21:10:50 +02:00
return cmListFileContext::FromListFileFunction(func, this->FileName);
2021-09-14 00:13:48 +02:00
}
stack.pop_back();
} else if (name == "function") {
stack.push_back({
NestingStateEnum::Function,
2022-03-29 21:10:50 +02:00
cmListFileContext::FromListFileFunction(func, this->FileName),
2021-09-14 00:13:48 +02:00
});
} else if (name == "endfunction") {
if (!TopIs(stack, NestingStateEnum::Function)) {
2022-03-29 21:10:50 +02:00
return cmListFileContext::FromListFileFunction(func, this->FileName);
2021-09-14 00:13:48 +02:00
}
stack.pop_back();
} else if (name == "macro") {
stack.push_back({
NestingStateEnum::Macro,
2022-03-29 21:10:50 +02:00
cmListFileContext::FromListFileFunction(func, this->FileName),
2021-09-14 00:13:48 +02:00
});
} else if (name == "endmacro") {
if (!TopIs(stack, NestingStateEnum::Macro)) {
2022-03-29 21:10:50 +02:00
return cmListFileContext::FromListFileFunction(func, this->FileName);
2021-09-14 00:13:48 +02:00
}
stack.pop_back();
2022-11-16 20:14:03 +01:00
} else if (name == "block") {
stack.push_back({
NestingStateEnum::Block,
cmListFileContext::FromListFileFunction(func, this->FileName),
});
} else if (name == "endblock") {
if (!TopIs(stack, NestingStateEnum::Block)) {
return cmListFileContext::FromListFileFunction(func, this->FileName);
}
stack.pop_back();
2021-09-14 00:13:48 +02:00
}
}
if (!stack.empty()) {
return stack.back().Context;
}
return cm::nullopt;
}
2022-08-04 22:12:04 +02:00
#include "cmConstStack.tcc"
template class cmConstStack<cmListFileContext, cmListFileBacktrace>;
2018-10-28 12:09:07 +01:00
std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
{
os << lfc.FilePath;
2021-09-14 00:13:48 +02:00
if (lfc.Line > 0) {
os << ":" << lfc.Line;
2016-07-09 11:21:54 +02:00
if (!lfc.Name.empty()) {
os << " (" << lfc.Name << ")";
}
2021-09-14 00:13:48 +02:00
} else if (lfc.Line == cmListFileContext::DeferPlaceholderLine) {
os << ":DEFERRED";
2016-07-09 11:21:54 +02:00
}
return os;
}
2015-08-17 11:37:30 +02:00
bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs)
{
2016-07-09 11:21:54 +02:00
if (lhs.Line != rhs.Line) {
2015-08-17 11:37:30 +02:00
return lhs.Line < rhs.Line;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
return lhs.FilePath < rhs.FilePath;
}
bool operator==(const cmListFileContext& lhs, const cmListFileContext& rhs)
{
return lhs.Line == rhs.Line && lhs.FilePath == rhs.FilePath;
}
bool operator!=(const cmListFileContext& lhs, const cmListFileContext& rhs)
{
return !(lhs == rhs);
}
2019-11-11 23:01:05 +01:00
std::ostream& operator<<(std::ostream& os, BT<std::string> const& s)
{
return os << s.Value;
}
2022-08-04 22:12:04 +02:00
std::vector<BT<std::string>> cmExpandListWithBacktrace(
2023-07-02 19:51:09 +02:00
std::string const& list, cmListFileBacktrace const& bt,
cmList::EmptyElements emptyArgs)
2019-11-11 23:01:05 +01:00
{
std::vector<BT<std::string>> result;
2023-07-02 19:51:09 +02:00
cmList tmp{ list, emptyArgs };
2019-11-11 23:01:05 +01:00
result.reserve(tmp.size());
for (std::string& i : tmp) {
result.emplace_back(std::move(i), bt);
}
return result;
}