Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 6d03959

Browse files
committed
Bug 767563 - Add a clang static checker, part 1: add the plugin shell. r=glandium
1 parent 920b3ea commit 6d03959

8 files changed

Lines changed: 186 additions & 0 deletions

File tree

build/clang-plugin/Makefile.in

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
CXX := @CXX@
6+
CXXFLAGS := @CXXFLAGS@
7+
LDFLAGS := @LDFLAGS@
8+
VPATH := @srcdir@
9+
10+
# Helper for end
11+
NULL :=
12+
13+
CPPSRCS := \
14+
clang-plugin.cpp \
15+
$(NULL)
16+
17+
TESTSRCS := \
18+
$(NULL)
19+
20+
OBJS := $(patsubst %.cpp,%.o,$(CPPSRCS))
21+
TESTS := $(patsubst %.cpp,test-%,$(TESTSRCS))
22+
23+
PLUGIN := libclang-plugin.so
24+
25+
all: $(PLUGIN) $(TESTS)
26+
27+
$(OBJS): %.o: %.cpp Makefile
28+
$(CXX) -o $@ -c $(CXXFLAGS) $<
29+
30+
$(PLUGIN): $(OBJS)
31+
rm -f $@
32+
$(CXX) -shared -o $@ $(CXXFLAGS) $(LDFLAGS) $(OBJS)
33+
34+
TESTFLAGS := -fsyntax-only -Xclang -verify \
35+
-Xclang -load -Xclang $(CURDIR)/$(PLUGIN) \
36+
-Xclang -add-plugin -Xclang moz-check
37+
38+
$(TESTS): test-%: tests/%.cpp $(PLUGIN)
39+
$(CXX) $(TESTFLAGS) $<
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
#include "clang/AST/ASTConsumer.h"
5+
#include "clang/AST/ASTContext.h"
6+
#include "clang/AST/RecursiveASTVisitor.h"
7+
#include "clang/Basic/Version.h"
8+
#include "clang/Frontend/CompilerInstance.h"
9+
#include "clang/Frontend/FrontendPluginRegistry.h"
10+
#include "clang/Sema/Sema.h"
11+
12+
#define CLANG_VERSION_FULL (CLANG_VERSION_MAJOR * 100 + CLANG_VERSION_MINOR)
13+
14+
using namespace llvm;
15+
using namespace clang;
16+
17+
namespace {
18+
class MozChecker : public ASTConsumer, public RecursiveASTVisitor<MozChecker> {
19+
DiagnosticsEngine &Diag;
20+
const CompilerInstance &CI;
21+
public:
22+
MozChecker(const CompilerInstance &CI) : Diag(CI.getDiagnostics()), CI(CI) {}
23+
virtual void HandleTranslationUnit(ASTContext &ctx) {
24+
TraverseDecl(ctx.getTranslationUnitDecl());
25+
}
26+
};
27+
28+
class MozCheckAction : public PluginASTAction {
29+
public:
30+
ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef fileName) {
31+
return new MozChecker(CI);
32+
}
33+
34+
bool ParseArgs(const CompilerInstance &CI,
35+
const std::vector<std::string> &args) {
36+
return true;
37+
}
38+
};
39+
}
40+
41+
static FrontendPluginRegistry::Add<MozCheckAction>
42+
X("moz-check", "check moz action");

build/clang-plugin/configure

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/sh
2+
3+
# Default srcdir to this directory
4+
srcdir=
5+
6+
for option; do
7+
case "$option" in
8+
-*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
9+
*) optarg= ;;
10+
esac
11+
12+
case "$option" in
13+
--srcdir=*) srcdir="$optarg";;
14+
esac
15+
done
16+
17+
if test -z "$CXX"; then
18+
CXX=`which clang++`
19+
fi
20+
21+
echo -n "checking for llvm-config... "
22+
23+
if test -z "$LLVMCONFIG"; then
24+
LLVMCONFIG=`which llvm-config`
25+
fi
26+
27+
if test -z "$LLVMCONFIG"; then
28+
LLVMCONFIG=`dirname $CXX`/llvm-config
29+
fi
30+
31+
if test ! -x "$LLVMCONFIG"; then
32+
echo "configure: error: Cannot find an llvm-config binary for building a clang plugin" 1>&2
33+
exit 1
34+
fi
35+
36+
echo "$LLVMCONFIG"
37+
38+
LLVMCXXFLAGS=`$LLVMCONFIG --cxxflags`
39+
LLVMLDFLAGS=`$LLVMCONFIG --ldflags`
40+
CXXFLAGS="$CXXFLAGS $LLVMCXXFLAGS -fno-rtti -fno-exceptions"
41+
LDFLAGS="$LDFLAGS $LLVMLDFLAGS"
42+
43+
cat $srcdir/Makefile.in | sed \
44+
-e "s%@CXX@%$CXX%" \
45+
-e "s%@CXXFLAGS@%$CXXFLAGS%" \
46+
-e "s%@LDFLAGS@%$LDFLAGS%" \
47+
-e "s%@srcdir@%$srcdir%" \
48+
> Makefile

config/static-checking-config.mk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ DEHYDRA_FLAGS = -fplugin=$(DEHYDRA_PATH) $(DEHYDRA_ARGS)
3636
ifdef DEHYDRA_PATH
3737
OS_CXXFLAGS += $(DEHYDRA_FLAGS)
3838
endif
39+
40+
ifdef ENABLE_CLANG_PLUGIN
41+
CLANG_PLUGIN := $(DEPTH)/build/clang-plugin/$(DLL_PREFIX)clang-plugin$(DLL_SUFFIX)
42+
OS_CXXFLAGS += -Xclang -load -Xclang $(CLANG_PLUGIN) -Xclang -add-plugin -Xclang moz-check
43+
OS_CFLAGS += -Xclang -load -Xclang $(CLANG_PLUGIN) -Xclang -add-plugin -Xclang moz-check
44+
endif

configure.in

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7464,6 +7464,23 @@ if test -n "$DEHYDRA_PATH"; then
74647464
fi
74657465
AC_SUBST(DEHYDRA_PATH)
74667466

7467+
dnl ========================================================
7468+
dnl = Enable using the clang plugin to build
7469+
dnl ========================================================
7470+
7471+
MOZ_ARG_ENABLE_BOOL(clang-plugin,
7472+
[ --enable-clang-plugin Enable building with the mozilla clang plugin ],
7473+
ENABLE_CLANG_PLUGIN=1,
7474+
ENABLE_CLANG_PLUGIN= )
7475+
if test -n "$ENABLE_CLANG_PLUGIN"; then
7476+
if test -z "$CLANG_CC"; then
7477+
AC_MSG_ERROR([Can't use clang plugin without clang.])
7478+
fi
7479+
AC_DEFINE(MOZ_CLANG_PLUGIN)
7480+
fi
7481+
7482+
AC_SUBST(ENABLE_CLANG_PLUGIN)
7483+
74677484
dnl ========================================================
74687485
dnl = Enable stripping of libs & executables
74697486
dnl ========================================================
@@ -9388,6 +9405,12 @@ HOST_CFLAGS="$_SUBDIR_HOST_CFLAGS"
93889405
HOST_LDFLAGS="$_SUBDIR_HOST_LDFLAGS"
93899406
RC=
93909407

9408+
if test -n "$ENABLE_CLANG_PLUGIN"; then
9409+
ac_configure_args="$_SUBDIR_CONFIG_ARGS"
9410+
AC_OUTPUT_SUBDIRS(build/clang-plugin)
9411+
fi
9412+
9413+
93919414
# Run the SpiderMonkey 'configure' script.
93929415
dist=$MOZ_BUILD_ROOT/dist
93939416
ac_configure_args="$_SUBDIR_CONFIG_ARGS"

js/src/config/static-checking-config.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ DEHYDRA_FLAGS = -fplugin=$(DEHYDRA_PATH) $(DEHYDRA_ARGS)
2626
ifdef DEHYDRA_PATH
2727
OS_CXXFLAGS += $(DEHYDRA_FLAGS)
2828
endif
29+
30+
ifdef ENABLE_CLANG_PLUGIN
31+
# Load the clang plugin from the mozilla topsrcdir. This implies that the clang
32+
# plugin is only usable if we're building js/src under mozilla/, though.
33+
CLANG_PLUGIN := $(DEPTH)/../../build/clang-plugin/$(DLL_PREFIX)clang-plugin$(DLL_SUFFIX)
34+
OS_CXXFLAGS += -fplugin=$(CLANG_PLUGIN)
35+
OS_CFLAGS += -fplugin=$(CLANG_PLUGIN)
36+
endif

js/src/configure.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,6 +3751,23 @@ if test -n "$DEHYDRA_PATH"; then
37513751
fi
37523752
AC_SUBST(DEHYDRA_PATH)
37533753

3754+
dnl ========================================================
3755+
dnl = Enable using the clang plugin to build
3756+
dnl ========================================================
3757+
3758+
MOZ_ARG_ENABLE_BOOL(clang-plugin,
3759+
[ --enable-clang-plugin Enable building with the mozilla clang plugin ],
3760+
ENABLE_CLANG_PLUGIN=1,
3761+
ENABLE_CLANG_PLUGIN= )
3762+
if test -n "$ENABLE_CLANG_PLUGIN"; then
3763+
if test -z "$CLANG_CC"; then
3764+
AC_MSG_ERROR([Can't use clang plugin without clang.])
3765+
fi
3766+
AC_DEFINE(MOZ_CLANG_PLUGIN)
3767+
fi
3768+
3769+
AC_SUBST(ENABLE_CLANG_PLUGIN)
3770+
37543771
dnl ========================================================
37553772
dnl = Enable static checking using sixgill
37563773
dnl ========================================================

moz.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
CONFIGURE_SUBST_FILES += ['tools/update-packaging/Makefile']
77

8+
if CONFIG['ENABLE_CLANG_PLUGIN']:
9+
add_tier_dir('base', 'build/clang-plugin', static=True)
10+
811
add_tier_dir('base', ['config', 'build', 'probes', 'mfbt', 'python'])
912

1013
if not CONFIG['LIBXUL_SDK']:

0 commit comments

Comments
 (0)