Skip to content

Commit

Permalink
Bug 1619921 - enable clang-plugin with support for alpha checkers mod…
Browse files Browse the repository at this point in the history
…ule. r=froydnj,sg

Differential Revision: https://phabricator.services.mozilla.com/D65314
  • Loading branch information
abpostelnicu committed Mar 17, 2020
1 parent cf80049 commit 7f54df3
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 9 deletions.
1 change: 1 addition & 0 deletions build/autoconf/clang-plugin.m4
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ AC_SUBST_LIST(LLVM_LDFLAGS)
AC_SUBST_LIST(CLANG_LDFLAGS)
AC_SUBST(ENABLE_CLANG_PLUGIN)
AC_SUBST(ENABLE_CLANG_PLUGIN_ALPHA)
AC_SUBST(ENABLE_MOZSEARCH_PLUGIN)
])
12 changes: 9 additions & 3 deletions build/build-clang/build-clang.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ def patch(patch, srcdir):
'-s'])


def import_clang_tidy(source_dir):
def import_clang_tidy(source_dir, build_clang_tidy_alpha):
clang_plugin_path = os.path.join(os.path.dirname(sys.argv[0]),
'..', 'clang-plugin')
clang_tidy_path = os.path.join(source_dir,
'clang-tools-extra/clang-tidy')
sys.path.append(clang_plugin_path)
from import_mozilla_checks import do_import
do_import(clang_plugin_path, clang_tidy_path)
do_import(clang_plugin_path, clang_tidy_path, build_clang_tidy_alpha)


def build_package(package_build_dir, cmake_args):
Expand Down Expand Up @@ -614,6 +614,12 @@ def prune_final_dir_for_clang_tidy(final_dir, osx_cross_compile):
build_clang_tidy = config["build_clang_tidy"]
if build_clang_tidy not in (True, False):
raise ValueError("Only boolean values are accepted for build_clang_tidy.")
build_clang_tidy_alpha = False
# check for build_clang_tidy_alpha only if build_clang_tidy is true
if build_clang_tidy and "build_clang_tidy_alpha" in config:
build_clang_tidy_alpha = config["build_clang_tidy_alpha"]
if build_clang_tidy_alpha not in (True, False):
raise ValueError("Only boolean values are accepted for build_clang_tidy_alpha.")
osx_cross_compile = False
if "osx_cross_compile" in config:
osx_cross_compile = config["osx_cross_compile"]
Expand Down Expand Up @@ -693,7 +699,7 @@ def prune_final_dir_for_clang_tidy(final_dir, osx_cross_compile):
package_name = "clang"
if build_clang_tidy:
package_name = "clang-tidy"
import_clang_tidy(source_dir)
import_clang_tidy(source_dir, build_clang_tidy_alpha)

if not os.path.exists(build_dir):
os.makedirs(build_dir)
Expand Down
3 changes: 3 additions & 0 deletions build/clang-plugin/DiagnosticsMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ DiagnosticsMatcher::DiagnosticsMatcher(CompilerInstance &CI) {
cls##_.registerPPCallbacks(CI);
#include "Checks.inc"
#include "external/ExternalChecks.inc"
#ifdef MOZ_CLANG_PLUGIN_ALPHA
#include "alpha/AlphaChecks.inc"
#endif
#undef CHECK
}
6 changes: 6 additions & 0 deletions build/clang-plugin/DiagnosticsMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "ChecksIncludes.inc"
#include "external/ExternalIncludes.inc"
#ifdef MOZ_CLANG_PLUGIN_ALPHA
#include "alpha/AlphaIncludes.inc"
#endif

class DiagnosticsMatcher {
public:
Expand All @@ -18,6 +21,9 @@ class DiagnosticsMatcher {
#define CHECK(cls, name) cls cls##_{name};
#include "Checks.inc"
#include "external/ExternalChecks.inc"
#ifdef MOZ_CLANG_PLUGIN_ALPHA
#include "alpha/AlphaChecks.inc"
#endif
#undef CHECK
MatchFinder AstMatcher;
};
Expand Down
3 changes: 3 additions & 0 deletions build/clang-plugin/MozillaTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class MozillaModule : public ClangTidyModule {
#define CHECK(cls, name) CheckFactories.registerCheck<cls>("mozilla-" name);
#include "Checks.inc"
#include "external/ExternalChecks.inc"
#ifdef MOZ_CLANG_PLUGIN_ALPHA
#include "alpha/AlphaChecks.inc"
#endif
#undef CHECK
}
};
Expand Down
8 changes: 8 additions & 0 deletions build/clang-plugin/alpha/AlphaChecks.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// The list of checker classes that are compatible with clang-tidy and are considered
// to be in alpha stage development.

// CHECK(AlphaChecker, "alpha-checker")
Empty file.
9 changes: 9 additions & 0 deletions build/clang-plugin/alpha/sources.mozbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

HOST_SOURCES += [
# 'AlphaChecker.cpp',
]
9 changes: 9 additions & 0 deletions build/clang-plugin/alpha/tests/sources.mozbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

SOURCES += [
# 'AlphaTest.cpp',
]
22 changes: 16 additions & 6 deletions build/clang-plugin/import_mozilla_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ def copy_dir_contents(src, dest):
raise Exception('Directory not copied. Error: %s' % e)


def write_cmake(module_path):
def write_cmake(module_path, import_alpha):
names = [' ' + os.path.basename(f) for f in glob.glob("%s/*.cpp" % module_path)]
names += [' ' + os.path.basename(f) for f in glob.glob("%s/external/*.cpp" % module_path)]
if import_alpha:
alpha_names = [' ' + os.path.join("alpha", os.path.basename(f))
for f in glob.glob("%s/alpha/*.cpp" % module_path)]
names += alpha_names
with open(os.path.join(module_path, 'CMakeLists.txt'), 'w') as f:
f.write("""set(LLVM_LINK_COMPONENTS support)
Expand Down Expand Up @@ -114,7 +118,7 @@ def generate_thread_allows(mozilla_path, module_path):
f.write(ThreadAllows.generate_allows({files, names}))


def do_import(mozilla_path, clang_tidy_path):
def do_import(mozilla_path, clang_tidy_path, import_alpha):
module = 'mozilla'
module_path = os.path.join(clang_tidy_path, module)
try:
Expand All @@ -126,7 +130,7 @@ def do_import(mozilla_path, clang_tidy_path):
copy_dir_contents(mozilla_path, module_path)
write_third_party_paths(mozilla_path, module_path)
generate_thread_allows(mozilla_path, module_path)
write_cmake(module_path)
write_cmake(module_path, import_alpha)
add_item_to_cmake_section(os.path.join(module_path, '..', 'plugin',
'CMakeLists.txt'),
'LINK_LIBS', 'clangTidyMozillaModule')
Expand All @@ -145,10 +149,11 @@ def do_import(mozilla_path, clang_tidy_path):


def main():
if len(sys.argv) != 3:
if len(sys.argv) < 3 or len(sys.argv) > 4:
print("""\
Usage: import_mozilla_checks.py <mozilla-clang-plugin-path> <clang-tidy-path>
Usage: import_mozilla_checks.py <mozilla-clang-plugin-path> <clang-tidy-path> [import_alpha]
Imports the Mozilla static analysis checks into a clang-tidy source tree.
If `import_alpha` is specified then in-tree alpha checkers will be also imported.
""")

return
Expand All @@ -161,7 +166,12 @@ def main():
if not os.path.isdir(mozilla_path):
print("Invalid path to clang-tidy source directory")

do_import(mozilla_path, clang_tidy_path)
import_alpha = False

if len(sys.argv) == 4 and sys.argv[3] == 'import_alpha':
import_alpha = True

do_import(mozilla_path, clang_tidy_path, import_alpha)


if __name__ == '__main__':
Expand Down
4 changes: 4 additions & 0 deletions build/clang-plugin/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ DIRS += [

include('external/sources.mozbuild')

if CONFIG['ENABLE_CLANG_PLUGIN_ALPHA']:
HOST_DEFINES["MOZ_CLANG_PLUGIN_ALPHA"] = "1"
include('alpha/sources.mozbuild')

# In the current moz.build world, we need to override essentially every
# variable to limit ourselves to what we need to build the clang plugin.
if CONFIG['HOST_OS_ARCH'] == 'WINNT':
Expand Down
4 changes: 4 additions & 0 deletions build/clang-plugin/tests/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ if CONFIG['OS_ARCH'] == 'WINNT':

include('../external/tests/sources.mozbuild')

if CONFIG['ENABLE_CLANG_PLUGIN_ALPHA']:
DEFINES["MOZ_CLANG_PLUGIN_ALPHA"] = "1"
include('../alpha/tests/sources.mozbuild')

DisableStlWrapping()
NoVisibilityFlags()

Expand Down
14 changes: 14 additions & 0 deletions build/moz.configure/toolchain.configure
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,20 @@ js_option('--enable-clang-plugin', env='ENABLE_CLANG_PLUGIN',
add_old_configure_assignment('ENABLE_CLANG_PLUGIN',
depends_if('--enable-clang-plugin')(lambda _: True))

js_option('--enable-clang-plugin-alpha', env='ENABLE_CLANG_PLUGIN_ALPHA',
help='Enable static analysis with clang-plugin alpha checks.')

@depends('--enable-clang-plugin', '--enable-clang-plugin-alpha')
def check_clang_plugin_alpha(enable_clang_plugin, enable_clang_plugin_alpha):
if enable_clang_plugin_alpha:
if enable_clang_plugin:
return True
die("Cannot enable clang-plugin alpha checkers without --enable-clang-plugin.")
return False

add_old_configure_assignment('ENABLE_CLANG_PLUGIN_ALPHA', check_clang_plugin_alpha)
set_define('MOZ_CLANG_PLUGIN_ALPHA', check_clang_plugin_alpha)

js_option('--enable-mozsearch-plugin', env='ENABLE_MOZSEARCH_PLUGIN',
help="Enable building with the mozsearch indexer plugin")

Expand Down
2 changes: 2 additions & 0 deletions docs/code-quality/static-analysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ If you want to build with the Firefox Clang plug-in
(located in ``/build/clang-plugin`` and associated with
``MOZ_CLANG_PLUGIN`` and the attributes in ``/mfbt/Attributes.h``)
just add ``--enable-clang-plugin`` to your mozconfig!
If you want to also have our experimental checkers that will produce ``warnings`` as
diagnostic messages also add ``--enable-clang-plugin-alpha``.
This requires to build Firefox using Clang.

Configuring the build environment
Expand Down

0 comments on commit 7f54df3

Please sign in to comment.