ENH: Added support for special variables CMAKE_CURRENT_LIST_FILE and CMAKE_CURRENT_LIST_LINE that evaluate to the file name and line number in which they appear. This implements the feature request from bug 1012.

This commit is contained in:
Brad King 2004-08-04 10:45:11 -04:00
parent 743eed068c
commit b6da1d1271
7 changed files with 92 additions and 20 deletions

View File

@ -356,7 +356,8 @@ int cmExecuteCommand(void *arg, const char *name,
for(int i = 0; i < numArgs; ++i) for(int i = 0; i < numArgs; ++i)
{ {
// Assume all arguments are quoted. // Assume all arguments are quoted.
lff.m_Arguments.push_back(cmListFileArgument(args[i], true)); lff.m_Arguments.push_back(cmListFileArgument(args[i], true,
"[CMake-Plugin]", 0));
} }
return mf->ExecuteCommand(lff); return mf->ExecuteCommand(lff);
} }

View File

@ -58,6 +58,8 @@ IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
cmSystemTools::ReplaceString(tmps, variable.c_str(), j->c_str()); cmSystemTools::ReplaceString(tmps, variable.c_str(), j->c_str());
arg.Value = tmps; arg.Value = tmps;
arg.Quoted = k->Quoted; arg.Quoted = k->Quoted;
arg.FilePath = k->FilePath;
arg.Line = k->Line;
newLFF.m_Arguments.push_back(arg); newLFF.m_Arguments.push_back(arg);
} }
mf.ExecuteCommand(newLFF); mf.ExecuteCommand(newLFF);

View File

@ -90,6 +90,9 @@ bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
return false; return false;
} }
// Get a pointer to a persistent copy of the name.
const char* filename = this->GetUniqueStringPointer(path);
// Create the scanner. // Create the scanner.
cmListFileLexer* lexer = cmListFileLexer_New(); cmListFileLexer* lexer = cmListFileLexer_New();
if(!lexer) if(!lexer)
@ -99,17 +102,17 @@ bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
} }
// Open the file. // Open the file.
if(!cmListFileLexer_SetFileName(lexer, path)) if(!cmListFileLexer_SetFileName(lexer, filename))
{ {
cmListFileLexer_Delete(lexer); cmListFileLexer_Delete(lexer);
cmSystemTools::Error("cmListFileCache: error can not open file ", path); cmSystemTools::Error("cmListFileCache: error can not open file ", filename);
return false; return false;
} }
// Use a simple recursive-descent parser to process the token // Use a simple recursive-descent parser to process the token
// stream. // stream.
cmListFile inFile; cmListFile inFile;
inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path); inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(filename);
bool parseError = false; bool parseError = false;
bool haveNewline = true; bool haveNewline = true;
cmListFileLexer_Token* token; cmListFileLexer_Token* token;
@ -126,9 +129,9 @@ bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
haveNewline = false; haveNewline = false;
cmListFileFunction inFunction; cmListFileFunction inFunction;
inFunction.m_Name = token->text; inFunction.m_Name = token->text;
inFunction.m_FilePath = path; inFunction.m_FilePath = filename;
inFunction.m_Line = token->line; inFunction.m_Line = token->line;
if(cmListFileCacheParseFunction(lexer, inFunction, path)) if(cmListFileCacheParseFunction(lexer, inFunction, filename))
{ {
inFile.m_Functions.push_back(inFunction); inFile.m_Functions.push_back(inFunction);
} }
@ -141,7 +144,7 @@ bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
{ {
cmOStringStream error; cmOStringStream error;
error << "Error in cmake code at\n" error << "Error in cmake code at\n"
<< path << ":" << token->line << ":\n" << filename << ":" << token->line << ":\n"
<< "Parse error. Expected a newline, got \"" << "Parse error. Expected a newline, got \""
<< token->text << "\"."; << token->text << "\".";
cmSystemTools::Error(error.str().c_str()); cmSystemTools::Error(error.str().c_str());
@ -152,7 +155,7 @@ bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
{ {
cmOStringStream error; cmOStringStream error;
error << "Error in cmake code at\n" error << "Error in cmake code at\n"
<< path << ":" << token->line << ":\n" << filename << ":" << token->line << ":\n"
<< "Parse error. Expected a command name, got \"" << "Parse error. Expected a command name, got \""
<< token->text << "\"."; << token->text << "\".";
cmSystemTools::Error(error.str().c_str()); cmSystemTools::Error(error.str().c_str());
@ -185,12 +188,12 @@ bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
{ {
cmListFileFunction project; cmListFileFunction project;
project.m_Name = "PROJECT"; project.m_Name = "PROJECT";
cmListFileArgument prj("Project", false); cmListFileArgument prj("Project", false, filename, 0);
project.m_Arguments.push_back(prj); project.m_Arguments.push_back(prj);
inFile.m_Functions.insert(inFile.m_Functions.begin(),project); inFile.m_Functions.insert(inFile.m_Functions.begin(),project);
} }
} }
m_ListFileCache[path] = inFile; m_ListFileCache[filename] = inFile;
return true; return true;
} }
@ -241,13 +244,13 @@ bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
token->type == cmListFileLexer_Token_ArgumentUnquoted) token->type == cmListFileLexer_Token_ArgumentUnquoted)
{ {
cmListFileArgument a(cmSystemTools::RemoveEscapes(token->text), cmListFileArgument a(cmSystemTools::RemoveEscapes(token->text),
false); false, filename, token->line);
function.m_Arguments.push_back(a); function.m_Arguments.push_back(a);
} }
else if(token->type == cmListFileLexer_Token_ArgumentQuoted) else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
{ {
cmListFileArgument a(cmSystemTools::RemoveEscapes(token->text), cmListFileArgument a(cmSystemTools::RemoveEscapes(token->text),
true); true, filename, token->line);
function.m_Arguments.push_back(a); function.m_Arguments.push_back(a);
} }
else if(token->type != cmListFileLexer_Token_Newline) else if(token->type != cmListFileLexer_Token_Newline)
@ -272,3 +275,26 @@ bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
return false; return false;
} }
//----------------------------------------------------------------------------
const char* cmListFileCache::GetUniqueStringPointer(const char* name)
{
UniqueStrings::iterator i = m_UniqueStrings.find(name);
if(i == m_UniqueStrings.end())
{
char* str = new char[strlen(name)+1];
strcpy(str, name);
i = m_UniqueStrings.insert(UniqueStrings::value_type(name, str)).first;
}
return i->second;
}
//----------------------------------------------------------------------------
cmListFileCache::~cmListFileCache()
{
for(UniqueStrings::iterator i = m_UniqueStrings.begin();
i != m_UniqueStrings.end(); ++i)
{
delete [] i->second;
}
}

View File

@ -28,9 +28,12 @@
struct cmListFileArgument struct cmListFileArgument
{ {
cmListFileArgument(): Value(), Quoted(false) {} cmListFileArgument(): Value(), Quoted(false), FilePath(0), Line(0) {}
cmListFileArgument(const cmListFileArgument& r): Value(r.Value), Quoted(r.Quoted) {} cmListFileArgument(const cmListFileArgument& r):
cmListFileArgument(const std::string& v, bool q): Value(v), Quoted(q) {} Value(r.Value), Quoted(r.Quoted), FilePath(r.FilePath), Line(r.Line) {}
cmListFileArgument(const std::string& v, bool q, const char* file,
long line): Value(v), Quoted(q),
FilePath(file), Line(line) {}
bool operator == (const cmListFileArgument& r) const bool operator == (const cmListFileArgument& r) const
{ {
return (this->Value == r.Value) && (this->Quoted == r.Quoted); return (this->Value == r.Value) && (this->Quoted == r.Quoted);
@ -41,13 +44,15 @@ struct cmListFileArgument
} }
std::string Value; std::string Value;
bool Quoted; bool Quoted;
const char* FilePath;
long Line;
}; };
struct cmListFileFunction struct cmListFileFunction
{ {
std::string m_Name; std::string m_Name;
std::vector<cmListFileArgument> m_Arguments; std::vector<cmListFileArgument> m_Arguments;
std::string m_FilePath; const char* m_FilePath;
long m_Line; long m_Line;
}; };
@ -80,12 +85,18 @@ public:
//! Flush cache file out of cache. //! Flush cache file out of cache.
void FlushCache(const char* path); void FlushCache(const char* path);
~cmListFileCache();
private: private:
// Cache the file // Cache the file
bool CacheFile(const char* path, bool requireProjectCommand); bool CacheFile(const char* path, bool requireProjectCommand);
// private data // private data
typedef std::map<cmStdString, cmListFile> ListFileMap; typedef std::map<cmStdString, cmListFile> ListFileMap;
ListFileMap m_ListFileCache; // file name to ListFile map ListFileMap m_ListFileCache; // file name to ListFile map
typedef std::map<cmStdString, char*> UniqueStrings;
UniqueStrings m_UniqueStrings;
const char* GetUniqueStringPointer(const char* name);
static cmListFileCache* Instance; // singelton pointer static cmListFileCache* Instance; // singelton pointer
}; };

View File

@ -173,6 +173,22 @@ IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
arg.Value = tmps; arg.Value = tmps;
arg.Quoted = k->Quoted; arg.Quoted = k->Quoted;
const char* def =
mf.GetDefinition("CMAKE_MACRO_REPORT_DEFINITION_LOCATION");
if(def && !cmSystemTools::IsOff(def))
{
// Report the location of the argument where the macro was
// defined.
arg.FilePath = k->FilePath;
arg.Line = k->Line;
}
else
{
// Report the location of the argument where the macro was
// invoked.
arg.FilePath = lff.m_FilePath;
arg.Line = lff.m_Line;
}
newLFF.m_Arguments.push_back(arg); newLFF.m_Arguments.push_back(arg);
} }
if(!mf.ExecuteCommand(newLFF)) if(!mf.ExecuteCommand(newLFF))

View File

@ -1423,7 +1423,9 @@ const char *cmMakefile::ExpandVariablesInString(std::string& source) const
const char *cmMakefile::ExpandVariablesInString(std::string& source, const char *cmMakefile::ExpandVariablesInString(std::string& source,
bool escapeQuotes, bool escapeQuotes,
bool atOnly) const bool atOnly,
const char* filename = 0,
long line = -1) const
{ {
// This method replaces ${VAR} and @VAR@ where VAR is looked up // This method replaces ${VAR} and @VAR@ where VAR is looked up
// with GetDefinition(), if not found in the map, nothing is expanded. // with GetDefinition(), if not found in the map, nothing is expanded.
@ -1542,6 +1544,18 @@ const char *cmMakefile::ExpandVariablesInString(std::string& source,
} }
found = true; found = true;
} }
else if(filename && (var == "CMAKE_CURRENT_LIST_FILE"))
{
result += filename;
found = true;
}
else if(line >= 0 && (var == "CMAKE_CURRENT_LIST_LINE"))
{
cmOStringStream ostr;
ostr << line;
result += ostr.str();
found = true;
}
} }
// if found add to result, if not, then it gets blanked // if found add to result, if not, then it gets blanked
if (!found) if (!found)
@ -1696,8 +1710,8 @@ void cmMakefile::ExpandArguments(
{ {
// Expand the variables in the argument. // Expand the variables in the argument.
value = i->Value; value = i->Value;
this->ExpandVariablesInString(value); this->ExpandVariablesInString(value, false, false, i->FilePath, i->Line);
// If the argument is quoted, it should be one argument. // If the argument is quoted, it should be one argument.
// Otherwise, it may be a list of arguments. // Otherwise, it may be a list of arguments.
if(i->Quoted) if(i->Quoted)

View File

@ -558,7 +558,9 @@ public:
*/ */
const char *ExpandVariablesInString(std::string& source) const; const char *ExpandVariablesInString(std::string& source) const;
const char *ExpandVariablesInString(std::string& source, bool escapeQuotes, const char *ExpandVariablesInString(std::string& source, bool escapeQuotes,
bool atOnly = false) const; bool atOnly = false,
const char* filename = 0,
long line = -1) const;
/** /**
* Remove any remaining variables in the string. Anything with ${var} or * Remove any remaining variables in the string. Anything with ${var} or