Bug 767563 - Add a clang static checker, part 1: add the plugin shell. r=glandium

This commit is contained in:
Joshua Cranmer 2013-03-23 21:12:25 -05:00
parent 57f90a3f4d
commit 951b7571e3
8 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,39 @@
# 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/.
CXX := @CXX@
CXXFLAGS := @CXXFLAGS@
LDFLAGS := @LDFLAGS@
VPATH := @srcdir@
# Helper for end
NULL :=
CPPSRCS := \
clang-plugin.cpp \
$(NULL)
TESTSRCS := \
$(NULL)
OBJS := $(patsubst %.cpp,%.o,$(CPPSRCS))
TESTS := $(patsubst %.cpp,test-%,$(TESTSRCS))
PLUGIN := libclang-plugin.so
all: $(PLUGIN) $(TESTS)
$(OBJS): %.o: %.cpp Makefile
$(CXX) -o $@ -c $(CXXFLAGS) $<
$(PLUGIN): $(OBJS)
rm -f $@
$(CXX) -shared -o $@ $(CXXFLAGS) $(LDFLAGS) $(OBJS)
TESTFLAGS := -fsyntax-only -Xclang -verify \
-Xclang -load -Xclang $(CURDIR)/$(PLUGIN) \
-Xclang -add-plugin -Xclang moz-check
$(TESTS): test-%: tests/%.cpp $(PLUGIN)
$(CXX) $(TESTFLAGS) $<

View File

@ -0,0 +1,42 @@
/* 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/. */
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Version.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Sema/Sema.h"
#define CLANG_VERSION_FULL (CLANG_VERSION_MAJOR * 100 + CLANG_VERSION_MINOR)
using namespace llvm;
using namespace clang;
namespace {
class MozChecker : public ASTConsumer, public RecursiveASTVisitor<MozChecker> {
DiagnosticsEngine &Diag;
const CompilerInstance &CI;
public:
MozChecker(const CompilerInstance &CI) : Diag(CI.getDiagnostics()), CI(CI) {}
virtual void HandleTranslationUnit(ASTContext &ctx) {
TraverseDecl(ctx.getTranslationUnitDecl());
}
};
class MozCheckAction : public PluginASTAction {
public:
ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef fileName) {
return new MozChecker(CI);
}
bool ParseArgs(const CompilerInstance &CI,
const std::vector<std::string> &args) {
return true;
}
};
}
static FrontendPluginRegistry::Add<MozCheckAction>
X("moz-check", "check moz action");

48
build/clang-plugin/configure vendored Executable file
View File

@ -0,0 +1,48 @@
#!/bin/sh
# Default srcdir to this directory
srcdir=
for option; do
case "$option" in
-*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) optarg= ;;
esac
case "$option" in
--srcdir=*) srcdir="$optarg";;
esac
done
if test -z "$CXX"; then
CXX=`which clang++`
fi
echo -n "checking for llvm-config... "
if test -z "$LLVMCONFIG"; then
LLVMCONFIG=`which llvm-config`
fi
if test -z "$LLVMCONFIG"; then
LLVMCONFIG=`dirname $CXX`/llvm-config
fi
if test ! -x "$LLVMCONFIG"; then
echo "configure: error: Cannot find an llvm-config binary for building a clang plugin" 1>&2
exit 1
fi
echo "$LLVMCONFIG"
LLVMCXXFLAGS=`$LLVMCONFIG --cxxflags`
LLVMLDFLAGS=`$LLVMCONFIG --ldflags`
CXXFLAGS="$CXXFLAGS $LLVMCXXFLAGS -fno-rtti -fno-exceptions"
LDFLAGS="$LDFLAGS $LLVMLDFLAGS"
cat $srcdir/Makefile.in | sed \
-e "s%@CXX@%$CXX%" \
-e "s%@CXXFLAGS@%$CXXFLAGS%" \
-e "s%@LDFLAGS@%$LDFLAGS%" \
-e "s%@srcdir@%$srcdir%" \
> Makefile

View File

@ -36,3 +36,9 @@ DEHYDRA_FLAGS = -fplugin=$(DEHYDRA_PATH) $(DEHYDRA_ARGS)
ifdef DEHYDRA_PATH
OS_CXXFLAGS += $(DEHYDRA_FLAGS)
endif
ifdef ENABLE_CLANG_PLUGIN
CLANG_PLUGIN := $(DEPTH)/build/clang-plugin/$(DLL_PREFIX)clang-plugin$(DLL_SUFFIX)
OS_CXXFLAGS += -Xclang -load -Xclang $(CLANG_PLUGIN) -Xclang -add-plugin -Xclang moz-check
OS_CFLAGS += -Xclang -load -Xclang $(CLANG_PLUGIN) -Xclang -add-plugin -Xclang moz-check
endif

View File

@ -7464,6 +7464,23 @@ if test -n "$DEHYDRA_PATH"; then
fi
AC_SUBST(DEHYDRA_PATH)
dnl ========================================================
dnl = Enable using the clang plugin to build
dnl ========================================================
MOZ_ARG_ENABLE_BOOL(clang-plugin,
[ --enable-clang-plugin Enable building with the mozilla clang plugin ],
ENABLE_CLANG_PLUGIN=1,
ENABLE_CLANG_PLUGIN= )
if test -n "$ENABLE_CLANG_PLUGIN"; then
if test -z "$CLANG_CC"; then
AC_MSG_ERROR([Can't use clang plugin without clang.])
fi
AC_DEFINE(MOZ_CLANG_PLUGIN)
fi
AC_SUBST(ENABLE_CLANG_PLUGIN)
dnl ========================================================
dnl = Enable stripping of libs & executables
dnl ========================================================
@ -9388,6 +9405,12 @@ HOST_CFLAGS="$_SUBDIR_HOST_CFLAGS"
HOST_LDFLAGS="$_SUBDIR_HOST_LDFLAGS"
RC=
if test -n "$ENABLE_CLANG_PLUGIN"; then
ac_configure_args="$_SUBDIR_CONFIG_ARGS"
AC_OUTPUT_SUBDIRS(build/clang-plugin)
fi
# Run the SpiderMonkey 'configure' script.
dist=$MOZ_BUILD_ROOT/dist
ac_configure_args="$_SUBDIR_CONFIG_ARGS"

View File

@ -26,3 +26,11 @@ DEHYDRA_FLAGS = -fplugin=$(DEHYDRA_PATH) $(DEHYDRA_ARGS)
ifdef DEHYDRA_PATH
OS_CXXFLAGS += $(DEHYDRA_FLAGS)
endif
ifdef ENABLE_CLANG_PLUGIN
# Load the clang plugin from the mozilla topsrcdir. This implies that the clang
# plugin is only usable if we're building js/src under mozilla/, though.
CLANG_PLUGIN := $(DEPTH)/../../build/clang-plugin/$(DLL_PREFIX)clang-plugin$(DLL_SUFFIX)
OS_CXXFLAGS += -fplugin=$(CLANG_PLUGIN)
OS_CFLAGS += -fplugin=$(CLANG_PLUGIN)
endif

View File

@ -3751,6 +3751,23 @@ if test -n "$DEHYDRA_PATH"; then
fi
AC_SUBST(DEHYDRA_PATH)
dnl ========================================================
dnl = Enable using the clang plugin to build
dnl ========================================================
MOZ_ARG_ENABLE_BOOL(clang-plugin,
[ --enable-clang-plugin Enable building with the mozilla clang plugin ],
ENABLE_CLANG_PLUGIN=1,
ENABLE_CLANG_PLUGIN= )
if test -n "$ENABLE_CLANG_PLUGIN"; then
if test -z "$CLANG_CC"; then
AC_MSG_ERROR([Can't use clang plugin without clang.])
fi
AC_DEFINE(MOZ_CLANG_PLUGIN)
fi
AC_SUBST(ENABLE_CLANG_PLUGIN)
dnl ========================================================
dnl = Enable static checking using sixgill
dnl ========================================================

View File

@ -5,6 +5,9 @@
CONFIGURE_SUBST_FILES += ['tools/update-packaging/Makefile']
if CONFIG['ENABLE_CLANG_PLUGIN']:
add_tier_dir('base', 'build/clang-plugin', static=True)
add_tier_dir('base', ['config', 'build', 'probes', 'mfbt', 'python'])
if not CONFIG['LIBXUL_SDK']: