소스 검색

WCDH: Ensure that the prefix argument to the macro is valid.

The prefix must be a C-identifier because it is written as the
prefix of preprocessor macros and possibly structs.
Stephen Kelly 11 년 전
부모
커밋
b47c125ff7

+ 8 - 0
Modules/WriteCompilerDetectionHeader.cmake

@@ -235,6 +235,14 @@ function(write_compiler_detection_header
     message(FATAL_ERROR "Unparsed arguments: ${_WCD_UNPARSED_ARGUMENTS}")
   endif()
 
+  if (prefix_arg STREQUAL "")
+    message(FATAL_ERROR "A prefix must be specified")
+  endif()
+  string(MAKE_C_IDENTIFIER ${prefix_arg} cleaned_prefix)
+  if (NOT prefix_arg STREQUAL cleaned_prefix)
+    message(FATAL_ERROR "The prefix must be a valid C identifier.")
+  endif()
+
   if(NOT _WCD_VERSION)
     set(_WCD_VERSION ${CMAKE_MINIMUM_REQUIRED_VERSION})
   endif()

+ 1 - 0
Tests/RunCMake/WriteCompilerDetectionHeader/EmptyPrefix-result.txt

@@ -0,0 +1 @@
+1

+ 5 - 0
Tests/RunCMake/WriteCompilerDetectionHeader/EmptyPrefix-stderr.txt

@@ -0,0 +1,5 @@
+CMake Error at .*/Modules/WriteCompilerDetectionHeader.cmake:[0-9]+ \(message\):
+  A prefix must be specified
+Call Stack \(most recent call first\):
+  EmptyPrefix.cmake:[0-9]+ \(write_compiler_detection_header\)
+  CMakeLists.txt:[0-9]+ \(include\)

+ 10 - 0
Tests/RunCMake/WriteCompilerDetectionHeader/EmptyPrefix.cmake

@@ -0,0 +1,10 @@
+
+include(WriteCompilerDetectionHeader)
+
+write_compiler_detection_header(
+  FILE "${CMAKE_CURRENT_BINARY_DIR}/somefile"
+  PREFIX ""
+  VERSION 3.1
+  COMPILERS GNU
+  FEATURES cxx_final
+)

+ 1 - 0
Tests/RunCMake/WriteCompilerDetectionHeader/InvalidPrefix-result.txt

@@ -0,0 +1 @@
+1

+ 5 - 0
Tests/RunCMake/WriteCompilerDetectionHeader/InvalidPrefix-stderr.txt

@@ -0,0 +1,5 @@
+CMake Error at .*/Modules/WriteCompilerDetectionHeader.cmake:[0-9]+ \(message\):
+  The prefix must be a valid C identifier.
+Call Stack \(most recent call first\):
+  InvalidPrefix.cmake:[0-9]+ \(write_compiler_detection_header\)
+  CMakeLists.txt:[0-9]+ \(include\)

+ 10 - 0
Tests/RunCMake/WriteCompilerDetectionHeader/InvalidPrefix.cmake

@@ -0,0 +1,10 @@
+
+include(WriteCompilerDetectionHeader)
+
+write_compiler_detection_header(
+  FILE "${CMAKE_CURRENT_BINARY_DIR}/somefile"
+  PREFIX "0compile"
+  VERSION 3.1
+  COMPILERS GNU
+  FEATURES cxx_final
+)

+ 2 - 0
Tests/RunCMake/WriteCompilerDetectionHeader/RunCMakeTest.cmake

@@ -10,3 +10,5 @@ run_cmake(OldVersion)
 run_cmake(InvalidCompiler)
 run_cmake(InvalidFeature)
 run_cmake(InvalidCXXFeature)
+run_cmake(EmptyPrefix)
+run_cmake(InvalidPrefix)