[cmake-developers] [PATCH v4 3/3] Reject break() without loop scope
Gregor Jasny
gjasny at googlemail.com
Mon Nov 17 14:00:54 EST 2014
Signed-off-by: Gregor Jasny <gjasny at googlemail.com>
---
Help/manual/cmake-policies.7.rst | 1 +
Help/policy/CMP0055.rst | 17 +++++++++++++
Source/cmBreakCommand.cxx | 33 ++++++++++++++++++++++++++
Source/cmPolicies.cxx | 5 ++++
Source/cmPolicies.h | 1 +
Tests/RunCMake/CMP0055/CMP0055-NEW-result.txt | 1 +
Tests/RunCMake/CMP0055/CMP0055-NEW-stderr.txt | 4 ++++
Tests/RunCMake/CMP0055/CMP0055-NEW.cmake | 4 ++++
Tests/RunCMake/CMP0055/CMP0055-OLD-result.txt | 1 +
Tests/RunCMake/CMP0055/CMP0055-OLD-stderr.txt | 1 +
Tests/RunCMake/CMP0055/CMP0055-OLD.cmake | 4 ++++
Tests/RunCMake/CMP0055/CMP0055-WARN-result.txt | 1 +
Tests/RunCMake/CMP0055/CMP0055-WARN-stderr.txt | 9 +++++++
Tests/RunCMake/CMP0055/CMP0055-WARN.cmake | 2 ++
Tests/RunCMake/CMP0055/CMakeLists.txt | 3 +++
Tests/RunCMake/CMP0055/RunCMakeTest.cmake | 5 ++++
Tests/RunCMake/CMakeLists.txt | 1 +
17 files changed, 93 insertions(+)
create mode 100644 Help/policy/CMP0055.rst
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-NEW-result.txt
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-NEW-stderr.txt
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-NEW.cmake
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-OLD-result.txt
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-OLD-stderr.txt
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-OLD.cmake
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-WARN-result.txt
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-WARN-stderr.txt
create mode 100644 Tests/RunCMake/CMP0055/CMP0055-WARN.cmake
create mode 100644 Tests/RunCMake/CMP0055/CMakeLists.txt
create mode 100644 Tests/RunCMake/CMP0055/RunCMakeTest.cmake
diff --git a/Help/manual/cmake-policies.7.rst b/Help/manual/cmake-policies.7.rst
index 7074bd5..742fd63 100644
--- a/Help/manual/cmake-policies.7.rst
+++ b/Help/manual/cmake-policies.7.rst
@@ -112,3 +112,4 @@ All Policies
/policy/CMP0052
/policy/CMP0053
/policy/CMP0054
+ /policy/CMP0055
diff --git a/Help/policy/CMP0055.rst b/Help/policy/CMP0055.rst
new file mode 100644
index 0000000..1c7783d
--- /dev/null
+++ b/Help/policy/CMP0055.rst
@@ -0,0 +1,17 @@
+CMP0055
+-------
+
+Disallow break() outside of loop contexts.
+
+CMake 3.1.0 and lower allowed to put a :command:`break` command outside of
+a loop context. This was undefined behavior.
+
+The OLD behavior for this policy is to allow
+:command:`break` to be placed outside of loop contexts.
+The NEW behavior for this policy is to issue an error if a misplaced break
+is found.
+
+This policy was introduced in CMake version 3.2.
+CMake version |release| warns when the policy is not set and uses
+OLD behavior. Use the cmake_policy command to set it to OLD or
+NEW explicitly.
diff --git a/Source/cmBreakCommand.cxx b/Source/cmBreakCommand.cxx
index b70e400..ae1781a 100644
--- a/Source/cmBreakCommand.cxx
+++ b/Source/cmBreakCommand.cxx
@@ -15,6 +15,39 @@
bool cmBreakCommand::InitialPass(std::vector<std::string> const&,
cmExecutionStatus &status)
{
+ if(!this->Makefile->IsLoopBlock())
+ {
+ bool issueMessage = true;
+ cmOStringStream e;
+ cmake::MessageType messageType = cmake::AUTHOR_WARNING;
+ switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0055))
+ {
+ case cmPolicies::WARN:
+ e << (this->Makefile->GetPolicies()
+ ->GetPolicyWarning(cmPolicies::CMP0055)) << "\n";
+ break;
+ case cmPolicies::OLD:
+ issueMessage = false;
+ break;
+ case cmPolicies::REQUIRED_ALWAYS:
+ case cmPolicies::REQUIRED_IF_USED:
+ case cmPolicies::NEW:
+ messageType = cmake::FATAL_ERROR;
+ break;
+ }
+
+ if (issueMessage)
+ {
+ e << "A BREAK command was found outside of a proper "
+ "FOREACH or WHILE loop scope.";
+ this->Makefile->IssueMessage(messageType, e.str());
+ if (messageType == cmake::FATAL_ERROR)
+ {
+ return false;
+ }
+ }
+ }
+
status.SetBreakInvoked(true);
return true;
}
diff --git a/Source/cmPolicies.cxx b/Source/cmPolicies.cxx
index a420f59..1527add 100644
--- a/Source/cmPolicies.cxx
+++ b/Source/cmPolicies.cxx
@@ -364,6 +364,11 @@ cmPolicies::cmPolicies()
CMP0054, "CMP0054",
"Only interpret if() arguments as variables or keywords when unquoted.",
3,1,0, cmPolicies::WARN);
+
+ this->DefinePolicy(
+ CMP0055, "CMP0055",
+ "Disallow break() outside of loop contexts.",
+ 3,2,0, cmPolicies::WARN);
}
cmPolicies::~cmPolicies()
diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h
index 7c73da8..5e0bea0 100644
--- a/Source/cmPolicies.h
+++ b/Source/cmPolicies.h
@@ -111,6 +111,7 @@ public:
CMP0053, ///< Simplify variable reference and escape sequence evaluation
CMP0054, ///< Only interpret if() arguments as variables
/// or keywords when unquoted.
+ CMP0055, ///< Disallow break() outside of loop contexts
/** \brief Always the last entry.
*
diff --git a/Tests/RunCMake/CMP0055/CMP0055-NEW-result.txt b/Tests/RunCMake/CMP0055/CMP0055-NEW-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-NEW-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/CMP0055/CMP0055-NEW-stderr.txt b/Tests/RunCMake/CMP0055/CMP0055-NEW-stderr.txt
new file mode 100644
index 0000000..421b5d3
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-NEW-stderr.txt
@@ -0,0 +1,4 @@
+CMake Error at CMP0055-NEW.cmake:4 \(break\):
+ A BREAK command was found outside of a proper FOREACH or WHILE loop scope.
+Call Stack \(most recent call first\):
+ CMakeLists.txt:3 \(include\)
diff --git a/Tests/RunCMake/CMP0055/CMP0055-NEW.cmake b/Tests/RunCMake/CMP0055/CMP0055-NEW.cmake
new file mode 100644
index 0000000..53ac214
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-NEW.cmake
@@ -0,0 +1,4 @@
+
+cmake_policy(SET CMP0055 NEW)
+
+break()
diff --git a/Tests/RunCMake/CMP0055/CMP0055-OLD-result.txt b/Tests/RunCMake/CMP0055/CMP0055-OLD-result.txt
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-OLD-result.txt
@@ -0,0 +1 @@
+0
diff --git a/Tests/RunCMake/CMP0055/CMP0055-OLD-stderr.txt b/Tests/RunCMake/CMP0055/CMP0055-OLD-stderr.txt
new file mode 100644
index 0000000..10f3293
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-OLD-stderr.txt
@@ -0,0 +1 @@
+^$
diff --git a/Tests/RunCMake/CMP0055/CMP0055-OLD.cmake b/Tests/RunCMake/CMP0055/CMP0055-OLD.cmake
new file mode 100644
index 0000000..57195c2
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-OLD.cmake
@@ -0,0 +1,4 @@
+
+cmake_policy(SET CMP0055 OLD)
+
+break()
diff --git a/Tests/RunCMake/CMP0055/CMP0055-WARN-result.txt b/Tests/RunCMake/CMP0055/CMP0055-WARN-result.txt
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-WARN-result.txt
@@ -0,0 +1 @@
+0
diff --git a/Tests/RunCMake/CMP0055/CMP0055-WARN-stderr.txt b/Tests/RunCMake/CMP0055/CMP0055-WARN-stderr.txt
new file mode 100644
index 0000000..6e74b1e
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-WARN-stderr.txt
@@ -0,0 +1,9 @@
+CMake Warning \(dev\) at CMP0055-WARN.cmake:2 \(break\):
+ Policy CMP0055 is not set: Disallow break\(\) outside of loop contexts. Run
+ "cmake --help-policy CMP0055" for policy details. Use the cmake_policy
+ command to set the policy and suppress this warning.
+
+ A BREAK command was found outside of a proper FOREACH or WHILE loop scope.
+Call Stack \(most recent call first\):
+ CMakeLists.txt:3 \(include\)
+This warning is for project developers. Use -Wno-dev to suppress it.
diff --git a/Tests/RunCMake/CMP0055/CMP0055-WARN.cmake b/Tests/RunCMake/CMP0055/CMP0055-WARN.cmake
new file mode 100644
index 0000000..373a95a
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMP0055-WARN.cmake
@@ -0,0 +1,2 @@
+
+break()
diff --git a/Tests/RunCMake/CMP0055/CMakeLists.txt b/Tests/RunCMake/CMP0055/CMakeLists.txt
new file mode 100644
index 0000000..2d75985
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/CMakeLists.txt
@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.1)
+project(${RunCMake_TEST} CXX)
+include(${RunCMake_TEST}.cmake)
diff --git a/Tests/RunCMake/CMP0055/RunCMakeTest.cmake b/Tests/RunCMake/CMP0055/RunCMakeTest.cmake
new file mode 100644
index 0000000..a033839
--- /dev/null
+++ b/Tests/RunCMake/CMP0055/RunCMakeTest.cmake
@@ -0,0 +1,5 @@
+include(RunCMake)
+
+run_cmake(CMP0055-OLD)
+run_cmake(CMP0055-NEW)
+run_cmake(CMP0055-WARN)
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt
index 24d0c93..ec7ddf7 100644
--- a/Tests/RunCMake/CMakeLists.txt
+++ b/Tests/RunCMake/CMakeLists.txt
@@ -49,6 +49,7 @@ add_RunCMake_test(CMP0050)
add_RunCMake_test(CMP0051)
add_RunCMake_test(CMP0053)
add_RunCMake_test(CMP0054)
+add_RunCMake_test(CMP0055)
add_RunCMake_test(CTest)
if(UNIX AND "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja")
add_RunCMake_test(CompilerChange)
--
1.9.3 (Apple Git-50)
More information about the cmake-developers
mailing list