cmake/Source/cmListFileCache.cxx

471 lines
13 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. */
#include "cmListFileCache.h"
#include "cmListFileLexer.h"
2016-10-30 18:24:19 +01:00
#include "cmMessenger.h"
2016-03-13 13:35:51 +01:00
#include "cmOutputConverter.h"
2017-04-14 19:02:05 +02:00
#include "cmState.h"
#include "cmSystemTools.h"
2016-10-30 18:24:19 +01:00
#include "cmake.h"
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h"
2016-10-30 18:24:19 +01:00
#include <algorithm>
#include <assert.h>
#include <sstream>
2013-11-03 12:27:13 +02:00
struct cmListFileParser
{
2017-07-20 19:35:53 +02:00
cmListFileParser(cmListFile* lf, cmListFileBacktrace const& lfbt,
2016-10-30 18:24:19 +01:00
cmMessenger* messenger, const char* filename);
2013-11-03 12:27:13 +02:00
~cmListFileParser();
2016-10-30 18:24:19 +01:00
void IssueFileOpenError(std::string const& text) const;
void IssueError(std::string const& text) const;
2013-11-03 12:27:13 +02:00
bool ParseFile();
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);
cmListFile* ListFile;
2016-10-30 18:24:19 +01:00
cmListFileBacktrace Backtrace;
cmMessenger* Messenger;
2013-11-03 12:27:13 +02:00
const char* FileName;
cmListFileLexer* Lexer;
cmListFileFunction Function;
2016-07-09 11:21:54 +02:00
enum
{
SeparationOkay,
SeparationWarning,
SeparationError
} Separation;
2013-11-03 12:27:13 +02:00
};
2017-07-20 19:35:53 +02:00
cmListFileParser::cmListFileParser(cmListFile* lf,
cmListFileBacktrace const& lfbt,
2016-10-30 18:24:19 +01:00
cmMessenger* messenger,
2016-07-09 11:21:54 +02:00
const char* filename)
: ListFile(lf)
2016-10-30 18:24:19 +01:00
, Backtrace(lfbt)
, Messenger(messenger)
2016-07-09 11:21:54 +02:00
, FileName(filename)
, 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
{
this->Messenger->IssueMessage(cmake::FATAL_ERROR, text, this->Backtrace);
}
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);
this->Messenger->IssueMessage(cmake::FATAL_ERROR, text, lfbt);
cmSystemTools::SetFatalErrorOccured();
}
2013-11-03 12:27:13 +02:00
bool cmListFileParser::ParseFile()
{
// Open the file.
2014-08-03 19:52:23 +02:00
cmListFileLexer_BOM bom;
2016-07-09 11:21:54 +02:00
if (!cmListFileLexer_SetFileName(this->Lexer, this->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) {
cmListFileLexer_SetFileName(this->Lexer, CM_NULLPTR, CM_NULLPTR);
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) {
2016-10-30 18:24:19 +01:00
cmListFileLexer_SetFileName(this->Lexer, CM_NULLPTR, CM_NULLPTR);
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
// 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)) {
2013-11-03 12:27:13 +02:00
this->ListFile->Functions.push_back(this->Function);
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
}
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;
{
2016-10-30 18:24:19 +01:00
cmListFileParser parser(this, lfbt, messenger, filename);
2016-07-09 11:21:54 +02:00
parseError = !parser.ParseFile();
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.
2013-11-03 12:27:13 +02:00
this->Function = cmListFileFunction();
this->Function.Name = name;
this->Function.Line = line;
// 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.
2013-11-03 12:27:13 +02:00
unsigned long lastLine;
unsigned long parenDepth = 0;
2013-11-03 12:27:13 +02:00
this->Separation = SeparationOkay;
2016-07-09 11:21:54 +02:00
while ((lastLine = cmListFileLexer_GetCurrentLine(this->Lexer),
token = cmListFileLexer_Scan(this->Lexer))) {
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) {
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;
lfc.Line = lastLine;
cmListFileBacktrace lfbt = this->Backtrace;
lfbt = lfbt.Push(lfc);
error << "Parse error. Function missing ending \")\". "
<< "End of file reached.";
2016-10-30 18:24:19 +01:00
this->Messenger->IssueMessage(cmake::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)
{
2015-11-17 17:22:37 +01:00
cmListFileArgument a(token->text, delim, token->line);
2013-11-03 12:27:13 +02:00
this->Function.Arguments.push_back(a);
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) {
2016-10-30 18:24:19 +01:00
this->Messenger->IssueMessage(cmake::FATAL_ERROR, m.str(), lfbt);
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
this->Messenger->IssueMessage(cmake::AUTHOR_WARNING, m.str(), lfbt);
return true;
2013-11-03 12:27:13 +02:00
}
2016-07-09 11:21:54 +02:00
struct cmListFileBacktrace::Entry : public cmListFileContext
2015-12-03 18:43:53 +01:00
{
2016-07-09 11:21:54 +02:00
Entry(cmListFileContext const& lfc, Entry* up)
: cmListFileContext(lfc)
, Up(up)
, RefCount(0)
{
if (this->Up) {
this->Up->Ref();
2015-12-03 18:43:53 +01:00
}
2016-07-09 11:21:54 +02:00
}
~Entry()
{
if (this->Up) {
this->Up->Unref();
}
}
void Ref() { ++this->RefCount; }
void Unref()
{
if (--this->RefCount == 0) {
delete this;
}
}
Entry* Up;
unsigned int RefCount;
};
2017-07-20 19:35:53 +02:00
cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& bottom,
Entry* up,
2016-07-09 11:21:54 +02:00
cmListFileContext const& lfc)
: Bottom(bottom)
, Cur(new Entry(lfc, up))
{
assert(this->Bottom.IsValid());
this->Cur->Ref();
}
2017-07-20 19:35:53 +02:00
cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& bottom,
Entry* cur)
2016-07-09 11:21:54 +02:00
: Bottom(bottom)
, Cur(cur)
{
if (this->Cur) {
assert(this->Bottom.IsValid());
this->Cur->Ref();
}
}
cmListFileBacktrace::cmListFileBacktrace()
: Bottom()
2016-10-30 18:24:19 +01:00
, Cur(CM_NULLPTR)
2016-07-09 11:21:54 +02:00
{
}
2017-07-20 19:35:53 +02:00
cmListFileBacktrace::cmListFileBacktrace(cmStateSnapshot const& snapshot)
2016-07-09 11:21:54 +02:00
: Bottom(snapshot.GetCallStackBottom())
2016-10-30 18:24:19 +01:00
, Cur(CM_NULLPTR)
2016-07-09 11:21:54 +02:00
{
}
cmListFileBacktrace::cmListFileBacktrace(cmListFileBacktrace const& r)
: Bottom(r.Bottom)
, Cur(r.Cur)
{
if (this->Cur) {
assert(this->Bottom.IsValid());
this->Cur->Ref();
}
}
cmListFileBacktrace& cmListFileBacktrace::operator=(
cmListFileBacktrace const& r)
{
cmListFileBacktrace tmp(r);
std::swap(this->Cur, tmp.Cur);
std::swap(this->Bottom, tmp.Bottom);
return *this;
2015-12-03 18:43:53 +01:00
}
cmListFileBacktrace::~cmListFileBacktrace()
{
2016-07-09 11:21:54 +02:00
if (this->Cur) {
this->Cur->Unref();
}
}
cmListFileBacktrace cmListFileBacktrace::Push(std::string const& file) const
{
// We are entering a file-level scope but have not yet reached
// any specific line or command invocation within it. This context
// is useful to print when it is at the top but otherwise can be
// skipped during call stack printing.
cmListFileContext lfc;
lfc.FilePath = file;
return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
}
cmListFileBacktrace cmListFileBacktrace::Push(
cmListFileContext const& lfc) const
{
return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
}
cmListFileBacktrace cmListFileBacktrace::Pop() const
{
assert(this->Cur);
return cmListFileBacktrace(this->Bottom, this->Cur->Up);
}
cmListFileContext const& cmListFileBacktrace::Top() const
{
if (this->Cur) {
return *this->Cur;
}
2016-10-30 18:24:19 +01:00
static cmListFileContext const empty;
return empty;
2015-12-03 18:43:53 +01:00
}
2015-11-17 17:22:37 +01:00
void cmListFileBacktrace::PrintTitle(std::ostream& out) const
2015-08-17 11:37:30 +02:00
{
2016-07-09 11:21:54 +02:00
if (!this->Cur) {
2015-04-27 22:25:09 +02:00
return;
2016-07-09 11:21:54 +02:00
}
cmOutputConverter converter(this->Bottom);
cmListFileContext lfc = *this->Cur;
if (!this->Bottom.GetState()->GetIsInTryCompile()) {
2016-10-30 18:24:19 +01:00
lfc.FilePath = converter.ConvertToRelativePath(
this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
out << (lfc.Line ? " at " : " in ") << lfc;
2015-04-27 22:25:09 +02:00
}
2015-11-17 17:22:37 +01:00
void cmListFileBacktrace::PrintCallStack(std::ostream& out) const
2015-08-17 11:37:30 +02:00
{
2016-07-09 11:21:54 +02:00
if (!this->Cur || !this->Cur->Up) {
2015-08-17 11:37:30 +02:00
return;
2016-07-09 11:21:54 +02:00
}
bool first = true;
cmOutputConverter converter(this->Bottom);
for (Entry* i = this->Cur->Up; i; i = i->Up) {
if (i->Name.empty()) {
// Skip this whole-file scope. When we get here we already will
// have printed a more-specific context within the file.
continue;
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
if (first) {
first = false;
out << "Call Stack (most recent call first):\n";
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
cmListFileContext lfc = *i;
if (!this->Bottom.GetState()->GetIsInTryCompile()) {
2016-10-30 18:24:19 +01:00
lfc.FilePath = converter.ConvertToRelativePath(
this->Bottom.GetState()->GetSourceDirectory(), lfc.FilePath);
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
out << " " << lfc << "\n";
}
2015-08-17 11:37:30 +02:00
}
2015-04-27 22:25:09 +02:00
std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
{
os << lfc.FilePath;
2016-07-09 11:21:54 +02:00
if (lfc.Line) {
os << ":" << lfc.Line;
2016-07-09 11:21:54 +02:00
if (!lfc.Name.empty()) {
os << " (" << lfc.Name << ")";
}
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);
}