[CMake] Add static analyzers
authordon.olmstead@sony.com <don.olmstead@sony.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 26 May 2020 19:29:29 +0000 (19:29 +0000)
committerdon.olmstead@sony.com <don.olmstead@sony.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 26 May 2020 19:29:29 +0000 (19:29 +0000)
https://bugs.webkit.org/show_bug.cgi?id=212280

Reviewed by David Kilzer.

.:

Add support for static analyzers within CMake builds. Supported analyzers are
clang-tidy, iwyu (include-what-you-use) and lwyu (link-what-you-use). They can
be enabled by passing a semicolon separated list to CMake through the ANALYZERS
option.

* Source/cmake/WebKitCommon.cmake:
* Source/cmake/WebKitFeatures.cmake:
* Source/cmake/WebKitMacros.cmake:
* Source/cmake/WebKitStaticAnalysis.cmake: Added.

Source/WTF:

Export headers from Plaform.h so include-what-you-use does not suggest they be
included directly. The headers being exported will #error when included outside of
Platform.h

* wtf/Platform.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@262156 268f45cc-cd09-0410-ab3c-d52691b4dbfc

ChangeLog
Source/WTF/ChangeLog
Source/WTF/wtf/Platform.h
Source/cmake/WebKitCommon.cmake
Source/cmake/WebKitFeatures.cmake
Source/cmake/WebKitMacros.cmake
Source/cmake/WebKitStaticAnalysis.cmake [new file with mode: 0644]

index e7e8589..49060f1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2020-05-26  Don Olmstead  <don.olmstead@sony.com>
+
+        [CMake] Add static analyzers
+        https://bugs.webkit.org/show_bug.cgi?id=212280
+
+        Reviewed by David Kilzer.
+
+        Add support for static analyzers within CMake builds. Supported analyzers are
+        clang-tidy, iwyu (include-what-you-use) and lwyu (link-what-you-use). They can
+        be enabled by passing a semicolon separated list to CMake through the ANALYZERS
+        option.
+
+        * Source/cmake/WebKitCommon.cmake:
+        * Source/cmake/WebKitFeatures.cmake:
+        * Source/cmake/WebKitMacros.cmake:
+        * Source/cmake/WebKitStaticAnalysis.cmake: Added.
+
 2020-05-26  Keith Rollin  <krollin@apple.com>
 
         Enable the use of XCBuild by default in Apple builds
index 433fce9..8941203 100644 (file)
@@ -1,3 +1,16 @@
+2020-05-26  Don Olmstead  <don.olmstead@sony.com>
+
+        [CMake] Add static analyzers
+        https://bugs.webkit.org/show_bug.cgi?id=212280
+
+        Reviewed by David Kilzer.
+
+        Export headers from Plaform.h so include-what-you-use does not suggest they be
+        included directly. The headers being exported will #error when included outside of
+        Platform.h
+
+        * wtf/Platform.h:
+
 2020-05-25  Alex Christensen  <achristensen@webkit.org>
 
         Expose more network metrics to WebCoreNSURLSession
index f7a4cb5..2d03b43 100644 (file)
@@ -32,7 +32,7 @@
 
 /* This ensures that users #include <wtf/Platform.h> rather than one of the helper files files directly. */
 #define WTF_PLATFORM_GUARD_AGAINST_INDIRECT_INCLUSION
-
+/* IWYU pragma: begin_exports */
 
 /* ==== Platform adaptation macros: these describe properties of the target environment. ==== */
 
@@ -73,7 +73,7 @@
 #include <WebKitAdditions/AdditionalPlatform.h>
 #endif
 
-
+/* IWYU pragma: end_exports */
 #undef WTF_PLATFORM_GUARD_AGAINST_INDIRECT_INCLUSION
 
 
index a27e27b..e629514 100644 (file)
@@ -50,6 +50,7 @@ if (NOT HAS_RUN_WEBKIT_COMMON)
     include(WebKitFS)
     include(WebKitCCache)
     include(WebKitCompilerFlags)
+    include(WebKitStaticAnalysis)
     include(WebKitFeatures)
     include(WebKitFindPackage)
 
index 9bcdb5d..7f624f3 100644 (file)
@@ -88,6 +88,13 @@ macro(WEBKIT_OPTION_BEGIN)
         set(ENABLE_SAMPLING_PROFILER_DEFAULT OFF)
     endif ()
 
+    if (DEFINED ClangTidy_EXE OR DEFINED IWYU_EXE)
+        message(STATUS "Unified builds are disabled when analyzing sources")
+        set(ENABLE_UNIFIED_BUILDS_DEFAULT OFF)
+    else ()
+        set(ENABLE_UNIFIED_BUILDS_DEFAULT ON)
+    endif ()
+
     WEBKIT_OPTION_DEFINE(ENABLE_3D_TRANSFORMS "Toggle 3D transforms support" PRIVATE ON)
     WEBKIT_OPTION_DEFINE(ENABLE_ACCELERATED_2D_CANVAS "Toggle Accelerated 2D Canvas support" PRIVATE OFF)
     WEBKIT_OPTION_DEFINE(ENABLE_ACCESSIBILITY "Whether to enable support for accessibility" PRIVATE ON)
@@ -208,7 +215,7 @@ macro(WEBKIT_OPTION_BEGIN)
     WEBKIT_OPTION_DEFINE(ENABLE_TEXT_AUTOSIZING "Toggle automatic text size adjustment support" PRIVATE OFF)
     WEBKIT_OPTION_DEFINE(ENABLE_TOUCH_EVENTS "Toggle Touch Events support" PRIVATE OFF)
     WEBKIT_OPTION_DEFINE(ENABLE_TOUCH_SLIDER "Toggle Touch Slider support" PRIVATE OFF)
-    WEBKIT_OPTION_DEFINE(ENABLE_UNIFIED_BUILDS "Toggle unified builds" PRIVATE ON)
+    WEBKIT_OPTION_DEFINE(ENABLE_UNIFIED_BUILDS "Toggle unified builds" PRIVATE ${ENABLE_UNIFIED_BUILDS_DEFAULT})
     WEBKIT_OPTION_DEFINE(ENABLE_USERSELECT_ALL "Toggle user-select:all support" PRIVATE ON)
     WEBKIT_OPTION_DEFINE(ENABLE_USER_MESSAGE_HANDLERS "Toggle user script message handler support" PRIVATE ON)
     WEBKIT_OPTION_DEFINE(ENABLE_VARIATION_FONTS "Toggle variation fonts support" PRIVATE OFF)
index c078d57..ed0ffc7 100644 (file)
@@ -180,8 +180,39 @@ macro(_WEBKIT_TARGET _target_logical_name _target_cmake_name)
     endif ()
 endmacro()
 
+macro(_WEBKIT_TARGET_ANALYZE _target)
+    if (ClangTidy_EXE)
+        set(_clang_path_and_options
+            ${ClangTidy_EXE}
+            # Include all non system headers
+            --header-filter=.*
+        )
+        set_target_properties(${_target} PROPERTIES
+            C_CLANG_TIDY "${_clang_path_and_options}"
+            CXX_CLANG_TIDY "${_clang_path_and_options}"
+        )
+    endif ()
+
+    if (IWYU_EXE)
+        set(_iwyu_path_and_options
+            ${IWYU_EXE}
+            # Suggests the more concise syntax introduced in C++17
+            -Xiwyu --cxx17ns
+            # Tells iwyu to always keep these includes
+            -Xiwyu --keep=**/config.h
+        )
+        if (MSVC)
+            list(APPEND _iwyu_path_and_options --driver-mode=cl)
+        endif ()
+        set_target_properties(${_target} PROPERTIES
+            CXX_INCLUDE_WHAT_YOU_USE "${_iwyu_path_and_options}"
+        )
+    endif ()
+endmacro()
+
 macro(WEBKIT_FRAMEWORK _target)
     _WEBKIT_TARGET(${_target} ${_target})
+    _WEBKIT_TARGET_ANALYZE(${_target})
 
     if (${_target}_OUTPUT_NAME)
         set_target_properties(${_target} PROPERTIES OUTPUT_NAME ${${_target}_OUTPUT_NAME})
@@ -213,6 +244,7 @@ endmacro()
 
 macro(WEBKIT_EXECUTABLE _target)
     _WEBKIT_TARGET(${_target} ${_target})
+    _WEBKIT_TARGET_ANALYZE(${_target})
 
     if (${_target}_OUTPUT_NAME)
         set_target_properties(${_target} PROPERTIES OUTPUT_NAME ${${_target}_OUTPUT_NAME})
@@ -243,6 +275,7 @@ macro(WEBKIT_WRAP_EXECUTABLE _target)
     add_library(${_wrapped_target_name} SHARED "${CMAKE_BINARY_DIR}/cmakeconfig.h")
 
     _WEBKIT_TARGET(${_target} ${_wrapped_target_name})
+    _WEBKIT_TARGET_ANALYZE(${_wrapped_target_name})
 
     # Unset values
     unset(${_target}_HEADERS)
diff --git a/Source/cmake/WebKitStaticAnalysis.cmake b/Source/cmake/WebKitStaticAnalysis.cmake
new file mode 100644 (file)
index 0000000..f42398b
--- /dev/null
@@ -0,0 +1,34 @@
+
+option(ANALYZERS "Static analysis tools to run on the build" "")
+
+if ("clang-tidy" IN_LIST ANALYZERS)
+    find_program(ClangTidy_EXE
+        NAMES clang-tidy
+        PATHS
+            "C:/Program Files/LLVM/bin"
+            "C:/Program Files (x86)/LLVM/bin"
+    )
+    if (ClangTidy_EXE)
+        message(STATUS "Found clang-tidy: ${ClangTidy_EXE}")
+    else ()
+        message(FATAL_ERROR "Could not find clang-tidy static analyzer")
+    endif ()
+endif ()
+
+if ("iwyu" IN_LIST ANALYZERS)
+    find_program(IWYU_EXE
+        NAMES iwyu include-what-you-use
+        PATHS
+            "C:/Program Files/include-what-you-use/bin"
+            "C:/Program Files (x86)/include-what-you-use/bin"
+    )
+    if (IWYU_EXE)
+        message(STATUS "Found include-what-you-use: ${IWYU_EXE}")
+    else ()
+        message(FAT "Could not find include-what-you-use static analyzer")
+    endif ()
+endif ()
+
+if ("lwyu" IN_LIST ANALYZERS)
+    set(CMAKE_LINK_WHAT_YOU_USE ON)
+endif ()