diff options
author | Gregor Jasny <gjasny@googlemail.com> | 2017-03-20 20:49:59 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2017-03-22 12:42:35 (GMT) |
commit | edac95b955afdea2d129b32b029bf845dc29cb71 (patch) | |
tree | cf96ba88c3e04fe23daa131817e92dd51515561a | |
parent | 3be5896e013492cbe1cf65c1c61822b68d9d59c4 (diff) | |
download | CMake-edac95b955afdea2d129b32b029bf845dc29cb71.zip CMake-edac95b955afdea2d129b32b029bf845dc29cb71.tar.gz CMake-edac95b955afdea2d129b32b029bf845dc29cb71.tar.bz2 |
cmIfCommand: Reject duplicate else() and misplaced elseif()
Closes: #14335
-rw-r--r-- | Source/cmIfCommand.cxx | 20 | ||||
-rw-r--r-- | Source/cmIfCommand.h | 2 | ||||
-rw-r--r-- | Tests/RunCMake/if/RunCMakeTest.cmake | 4 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-deep-else-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-deep-else-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-deep-else.cmake | 7 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-else-after-elseif-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-else-after-elseif-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-else-after-elseif.cmake | 5 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-else-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-else-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/if/duplicate-else.cmake | 4 | ||||
-rw-r--r-- | Tests/RunCMake/if/misplaced-elseif-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/if/misplaced-elseif-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/if/misplaced-elseif.cmake | 4 |
15 files changed, 66 insertions, 0 deletions
diff --git a/Source/cmIfCommand.cxx b/Source/cmIfCommand.cxx index a8fa4f9..421c3dd 100644 --- a/Source/cmIfCommand.cxx +++ b/Source/cmIfCommand.cxx @@ -57,8 +57,19 @@ bool cmIfFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff, // watch for our state change if (scopeDepth == 0 && !cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "else")) { + + if (this->ElseSeen) { + cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]); + mf.GetCMakeInstance()->IssueMessage( + cmake::FATAL_ERROR, + "A duplicate ELSE command was found inside an IF block.", bt); + cmSystemTools::SetFatalErrorOccured(); + return true; + } + this->IsBlocking = this->HasRun; this->HasRun = true; + this->ElseSeen = true; // if trace is enabled, print a (trivially) evaluated "else" // statement @@ -68,6 +79,15 @@ bool cmIfFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff, } else if (scopeDepth == 0 && !cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "elseif")) { + if (this->ElseSeen) { + cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]); + mf.GetCMakeInstance()->IssueMessage( + cmake::FATAL_ERROR, + "An ELSEIF command was found after an ELSE command.", bt); + cmSystemTools::SetFatalErrorOccured(); + return true; + } + if (this->HasRun) { this->IsBlocking = true; } else { diff --git a/Source/cmIfCommand.h b/Source/cmIfCommand.h index 56eef30..f81db67 100644 --- a/Source/cmIfCommand.h +++ b/Source/cmIfCommand.h @@ -21,6 +21,7 @@ public: cmIfFunctionBlocker() { this->HasRun = false; + this->ElseSeen = false; this->ScopeDepth = 0; } ~cmIfFunctionBlocker() CM_OVERRIDE {} @@ -32,6 +33,7 @@ public: std::vector<cmListFileFunction> Functions; bool IsBlocking; bool HasRun; + bool ElseSeen; unsigned int ScopeDepth; }; diff --git a/Tests/RunCMake/if/RunCMakeTest.cmake b/Tests/RunCMake/if/RunCMakeTest.cmake index 077d00a..f54edf7 100644 --- a/Tests/RunCMake/if/RunCMakeTest.cmake +++ b/Tests/RunCMake/if/RunCMakeTest.cmake @@ -3,7 +3,11 @@ include(RunCMake) run_cmake(InvalidArgument1) run_cmake(IsDirectory) run_cmake(IsDirectoryLong) +run_cmake(duplicate-deep-else) +run_cmake(duplicate-else) +run_cmake(duplicate-else-after-elseif) run_cmake(elseif-message) +run_cmake(misplaced-elseif) run_cmake(MatchesSelf) diff --git a/Tests/RunCMake/if/duplicate-deep-else-result.txt b/Tests/RunCMake/if/duplicate-deep-else-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/if/duplicate-deep-else-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/if/duplicate-deep-else-stderr.txt b/Tests/RunCMake/if/duplicate-deep-else-stderr.txt new file mode 100644 index 0000000..ac2335c --- /dev/null +++ b/Tests/RunCMake/if/duplicate-deep-else-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at duplicate-deep-else.cmake:[0-9]+ \(else\): + A duplicate ELSE command was found inside an IF block. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/if/duplicate-deep-else.cmake b/Tests/RunCMake/if/duplicate-deep-else.cmake new file mode 100644 index 0000000..94f06de --- /dev/null +++ b/Tests/RunCMake/if/duplicate-deep-else.cmake @@ -0,0 +1,7 @@ +if(0) +else() + if(0) + else() + else() + endif() +endif() diff --git a/Tests/RunCMake/if/duplicate-else-after-elseif-result.txt b/Tests/RunCMake/if/duplicate-else-after-elseif-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/if/duplicate-else-after-elseif-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/if/duplicate-else-after-elseif-stderr.txt b/Tests/RunCMake/if/duplicate-else-after-elseif-stderr.txt new file mode 100644 index 0000000..ba6765c --- /dev/null +++ b/Tests/RunCMake/if/duplicate-else-after-elseif-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at duplicate-else-after-elseif.cmake:[0-9]+ \(else\): + A duplicate ELSE command was found inside an IF block. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/if/duplicate-else-after-elseif.cmake b/Tests/RunCMake/if/duplicate-else-after-elseif.cmake new file mode 100644 index 0000000..d1d4ac1 --- /dev/null +++ b/Tests/RunCMake/if/duplicate-else-after-elseif.cmake @@ -0,0 +1,5 @@ +if(0) +elseif(0) +else() +else() +endif() diff --git a/Tests/RunCMake/if/duplicate-else-result.txt b/Tests/RunCMake/if/duplicate-else-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/if/duplicate-else-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/if/duplicate-else-stderr.txt b/Tests/RunCMake/if/duplicate-else-stderr.txt new file mode 100644 index 0000000..e0dd01f --- /dev/null +++ b/Tests/RunCMake/if/duplicate-else-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at duplicate-else.cmake:[0-9]+ \(else\): + A duplicate ELSE command was found inside an IF block. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/if/duplicate-else.cmake b/Tests/RunCMake/if/duplicate-else.cmake new file mode 100644 index 0000000..14a03ac --- /dev/null +++ b/Tests/RunCMake/if/duplicate-else.cmake @@ -0,0 +1,4 @@ +if(0) +else() +else() +endif() diff --git a/Tests/RunCMake/if/misplaced-elseif-result.txt b/Tests/RunCMake/if/misplaced-elseif-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/if/misplaced-elseif-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/if/misplaced-elseif-stderr.txt b/Tests/RunCMake/if/misplaced-elseif-stderr.txt new file mode 100644 index 0000000..c4b0266 --- /dev/null +++ b/Tests/RunCMake/if/misplaced-elseif-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at misplaced-elseif.cmake:[0-9]+ \(elseif\): + An ELSEIF command was found after an ELSE command. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/if/misplaced-elseif.cmake b/Tests/RunCMake/if/misplaced-elseif.cmake new file mode 100644 index 0000000..d27f24e --- /dev/null +++ b/Tests/RunCMake/if/misplaced-elseif.cmake @@ -0,0 +1,4 @@ +if(0) +else() +elseif(1) +endif() |