Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing warnings on subsequent CMake runs #30

Open
LeSpocky opened this issue Apr 23, 2021 · 5 comments
Open

Missing warnings on subsequent CMake runs #30

LeSpocky opened this issue Apr 23, 2021 · 5 comments

Comments

@LeSpocky
Copy link
Contributor

When building with GCC and enabling SANITIZE_MEMORY there are only warnings on missing compiler support on the very first run of CMake. There's no warning nor error on missing compiler support once CMake cache is in place. This is bad, because it leads to the false impression everything is fine and the sanitizer is in place. This might lead to thinking code has no flaws.

You can simply reproduce this by building the example from https://github.com/google/sanitizers/wiki/MemorySanitizer with GCC and enable SANITIZE_MEMORY, and run cmake again on the same build folder. Everythings builds fine, no warning at buildtime nor runtime, although the code is clearly buggy and MemorySanitizer with Clang finds it.

The documentation in README.md states:

If your compiler has no sanitizing capabilities (I asume intel compiler doesn't) you'll get a warning but CMake will continue processing and sanitizing will simply just be ignored.

However this is only the case on the first CMake run, there is no warning on subsequent runs when cache is populated. But there should be!

@rseragon
Copy link

rseragon commented Mar 7, 2022

This is a pretty annoying problem, but can be easily fixed by adding a check in the add_sanitize_x method. I've made a quick fix for it in my fork, feel free to take a look into it.

@LeSpocky
Copy link
Contributor Author

LeSpocky commented Mar 7, 2022

Had a quick look in my local fork, and saw I had done something sligthly different:

diff --git a/cmake/FindSanitizers.cmake b/cmake/FindSanitizers.cmake
index 6960482..eb1c721 100755
--- a/cmake/FindSanitizers.cmake
+++ b/cmake/FindSanitizers.cmake
@@ -26,6 +26,7 @@
 # link against the sanitizers.
 option(SANITIZE_LINK_STATIC "Try to link static against sanitizers." Off)
 
+option(Sanitizers_SUPPRESS_WARNINGS "Suppress Sanitizers module warnings" OFF)
 
 
 
@@ -64,25 +65,31 @@ function(add_sanitizers ...)
         # wise sanitizers can't be used and a warning should be printed once.
         get_target_property(TARGET_TYPE ${TARGET} TYPE)
         if (TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
-            message(WARNING "Can't use any sanitizers for target ${TARGET}, "
+            if(NOT Sanitizers_SUPPRESS_WARNINGS)
+                message(WARNING "Can't use any sanitizers for target ${TARGET}, "
                     "because it is an interface library and cannot be "
                     "compiled directly.")
+            endif()
             continue()
         endif ()
         sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
         list(LENGTH TARGET_COMPILER NUM_COMPILERS)
         if (NUM_COMPILERS GREATER 1)
-            message(WARNING "Can't use any sanitizers for target ${TARGET}, "
+            if(NOT Sanitizers_SUPPRESS_WARNINGS)
+                message(WARNING "Can't use any sanitizers for target ${TARGET}, "
                     "because it will be compiled by incompatible compilers. "
                     "Target will be compiled without sanitizers.")
+            endif()
             continue()
 
         # If the target is compiled by no or no known compiler, give a warning.
         elseif (NUM_COMPILERS EQUAL 0)
-            message(WARNING "Sanitizers for target ${TARGET} may not be"
-                    " usable, because it uses no or an unknown compiler. "
+            if(NOT Sanitizers_SUPPRESS_WARNINGS)
+                message(WARNING "Sanitizers for target ${TARGET} may not be "
+                    "usable, because it uses no or an unknown compiler. "
                     "This is a false warning for targets using only "
-           "object lib(s) as input.")
+                    "object lib(s) as input.")
+            endif()
         endif ()
 
         # Add sanitizers for target.

@rseragon
Copy link

rseragon commented Mar 7, 2022

From what I can see, you've only suppressed the warnings using a cache variable, but not checking if the flags were detected and added to the targets. This would raise the same problem of not showing the warnings when a compiler doesn't support a specific sanitizer in the successive runs.
What about the other files? does your cache variables bring modification to any of them?

@LeSpocky
Copy link
Contributor Author

LeSpocky commented Mar 7, 2022

From what I can see, you've only suppressed the warnings using a cache variable, but not checking if the flags were detected and added to the targets. This would raise the same problem of not showing the warnings when a compiler doesn't support a specific sanitizer in the successive runs.

Sorry, I mixed up things here. The diff from above does not help at all with the reported problem, but does the opposite. 🙄

What about the other files? does your cache variables bring modification to any of them?

No.

@rseragon
Copy link

I see. You can still look into my fork if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants