Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bug 767563 - Add a clang static checker, part 1: add the plugin shell…

…. r=glandium
  • Loading branch information...
commit 114a094ee5d05ff0394dd1cf49c6efb535799801 1 parent e1739f3
@jcranmer jcranmer authored
View
39 build/clang-plugin/Makefile.in
@@ -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
42 build/clang-plugin/clang-plugin.cpp
@@ -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");
View
48 build/clang-plugin/configure
@@ -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
6 config/static-checking-config.mk
@@ -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
23 configure.in
@@ -7465,6 +7465,23 @@ 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 ========================================================
MOZ_ARG_ENABLE_BOOL(strip,
@@ -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
8 js/src/config/static-checking-config.mk
@@ -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
17 js/src/configure.in
@@ -3752,6 +3752,23 @@ 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
3  moz.build
@@ -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']:
Please sign in to comment.
Something went wrong with that request. Please try again.