Print line number of cache parse errors (#11109)

Track the line number while parsing `CMakeCache.txt` files and include
it in a parse failure error message.
This commit is contained in:
Ashley Whetter 2016-01-17 22:09:30 +00:00 committed by Brad King
parent 249aac71d0
commit 77cd74a3ea
8 changed files with 34 additions and 2 deletions

View File

@ -64,12 +64,14 @@ bool cmCacheManager::LoadCache(const std::string& path,
const char *realbuffer; const char *realbuffer;
std::string buffer; std::string buffer;
std::string entryKey; std::string entryKey;
unsigned int lineno = 0;
while(fin) while(fin)
{ {
// Format is key:type=value // Format is key:type=value
std::string helpString; std::string helpString;
CacheEntry e; CacheEntry e;
cmSystemTools::GetLineFromStream(fin, buffer); cmSystemTools::GetLineFromStream(fin, buffer);
lineno++;
realbuffer = buffer.c_str(); realbuffer = buffer.c_str();
while(*realbuffer != '0' && while(*realbuffer != '0' &&
(*realbuffer == ' ' || (*realbuffer == ' ' ||
@ -77,6 +79,7 @@ bool cmCacheManager::LoadCache(const std::string& path,
*realbuffer == '\r' || *realbuffer == '\r' ||
*realbuffer == '\n')) *realbuffer == '\n'))
{ {
if (*realbuffer == '\n') lineno++;
realbuffer++; realbuffer++;
} }
// skip blank lines and comment lines // skip blank lines and comment lines
@ -96,6 +99,7 @@ bool cmCacheManager::LoadCache(const std::string& path,
helpString += &realbuffer[2]; helpString += &realbuffer[2];
} }
cmSystemTools::GetLineFromStream(fin, buffer); cmSystemTools::GetLineFromStream(fin, buffer);
lineno++;
realbuffer = buffer.c_str(); realbuffer = buffer.c_str();
if(!fin) if(!fin)
{ {
@ -138,8 +142,10 @@ bool cmCacheManager::LoadCache(const std::string& path,
} }
else else
{ {
cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str(), std::ostringstream error;
". Offending entry: ", realbuffer); error << "Parse error in cache file " << cacheFile;
error << " on line " << lineno << ". Offending entry: " << realbuffer;
cmSystemTools::Error(error.str().c_str());
} }
} }
this->CacheMajorVersion = 0; this->CacheMajorVersion = 0;

View File

@ -33,6 +33,11 @@ run_cmake_command(build-bad-dir
run_cmake_command(build-bad-generator run_cmake_command(build-bad-generator
${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR}/cache-bad-generator) ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR}/cache-bad-generator)
run_cmake_command(cache-bad-entry
${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR}/cache-bad-entry/)
run_cmake_command(cache-empty-entry
${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR}/cache-empty-entry/)
function(run_BuildDir) function(run_BuildDir)
# Use a single build tree for a few tests without cleaning. # Use a single build tree for a few tests without cleaning.
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/BuildDir-build) set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/BuildDir-build)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
^CMake Error: Parse error in cache file .*/Tests/RunCMake/CommandLine/cache-bad-entry/CMakeCache.txt on line 7. Offending entry: BAD ENTRY.*

View File

@ -0,0 +1,10 @@
# This is a comment
// That was an empty line. This isn't.
EMPTY_LINE:BOOL=FALSE
// Uhoh! Here it comes
BAD ENTRY
// This is fine
GOOD_ENTRY:BOOL=TRUE

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
^CMake Error: Parse error in cache file .*/Tests/RunCMake/CommandLine/cache-empty-entry/CMakeCache.txt on line 5. Offending entry:.*

View File

@ -0,0 +1,7 @@
// This is valid
VALID:BOOL=TRUE
// This isn't
// One final entry
FINAL:BOOL=TRUE