diff --git a/README.md b/README.md index 5d45906..96fdc24 100644 --- a/README.md +++ b/README.md @@ -281,7 +281,7 @@ $Clang_DIR/bin/clang -cc1 -load /lib/libLACommenter.dylib -plugin LAC ``` ### Run the plugin through `ct-la-commenter` -**locommenter** is a standalone tool that will run the **LACommenter** plugin, +**lacommenter** is a standalone tool that will run the **LACommenter** plugin, but without the need of using `clang` and loading the plugin: ```bash @@ -358,6 +358,30 @@ the warnings with correct source code information. `-fcolor-diagnostics` above instructs Clang to generate color output (unfortunately Markdown doesn't render the colors here). +According to [Clang Plugins](https://clang.llvm.org/docs/ClangPlugins.html#using-the-clang-command-line) +> If the plugin class implements the `getActionType` method then the plugin is run automatically. +```c +// Automatically run the plugin after the main AST action +PluginASTAction::ActionType getActionType() override { + return AddAfterMainAction; +} +``` + +The **CodeStyleChecker** plugin could be automatically load and be used during the normal compilation +process to detect errors in the code, and get the output file, for example: +```bash +$Clang_DIR/bin/clang -fplugin=libCodeStyleChecker.dylib -o file.o -c file.cpp +file.cpp:2:7: warning: Type and variable names should start with upper-case letter +class clangTutor_BadName; + ^~~~~~~~~~~~~~~~~~~ + ClangTutor_BadName +file.cpp:2:17: warning: `_` in names is not allowed +class clangTutor_BadName; + ~~~~~~~~~~^~~~~~~~~ + clangTutorBadName +2 warnings generated. +``` + ### Run the plugin through `ct-code-style-checker` **ct-code-style-checker** is a standalone tool that will run the **CodeStyleChecker** plugin, but without the need of using `clang` and loading the plugin: diff --git a/lib/CodeStyleChecker.cpp b/lib/CodeStyleChecker.cpp index f368d15..17aea69 100644 --- a/lib/CodeStyleChecker.cpp +++ b/lib/CodeStyleChecker.cpp @@ -193,6 +193,14 @@ class CSCASTAction : public PluginASTAction { ros << "Help for CodeStyleChecker plugin goes here\n"; } + PluginASTAction::ActionType getActionType() override { +#ifndef TARGET_CLANG_TOOL + return AddBeforeMainAction; +#else + return CmdlineAfterMainAction; +#endif + } + private: bool MainTuOnly = true; }; diff --git a/test/CodeStyleCheckerAnonymous.cpp b/test/CodeStyleCheckerAnonymous.cpp index 9f816ed..340221f 100644 --- a/test/CodeStyleCheckerAnonymous.cpp +++ b/test/CodeStyleCheckerAnonymous.cpp @@ -1,4 +1,6 @@ -// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -plugin CSC %s 2>&1 +// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -add-plugin CSC %s 2>&1 +// RUN: clang -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -c %s 2>&1 +// RUN: clang -fplugin=%shlibdir/libCodeStyleChecker%shlibext -c %s 2>&1 // 1. Verify that anonymous unions are not flagged as invalid (no name -> // nothing to check). However, the member variables _are_ verified. diff --git a/test/CodeStyleCheckerConversionOp.cpp b/test/CodeStyleCheckerConversionOp.cpp index bfbcbc5..4e31631 100644 --- a/test/CodeStyleCheckerConversionOp.cpp +++ b/test/CodeStyleCheckerConversionOp.cpp @@ -1,4 +1,6 @@ -// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -plugin CSC %s 2>&1 +// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -add-plugin CSC %s 2>&1 +// RUN: clang -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -c %s 2>&1 +// RUN: clang -fplugin=%shlibdir/libCodeStyleChecker%shlibext -c %s 2>&1 // Verify that conversion operators are not checked diff --git a/test/CodeStyleCheckerFunction.cpp b/test/CodeStyleCheckerFunction.cpp index 68a56f6..5872802 100644 --- a/test/CodeStyleCheckerFunction.cpp +++ b/test/CodeStyleCheckerFunction.cpp @@ -1,4 +1,6 @@ -// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -plugin CSC %s 2>&1 +// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -add-plugin CSC %s 2>&1 +// RUN: clang -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -c %s 2>&1 +// RUN: clang -fplugin=%shlibdir/libCodeStyleChecker%shlibext -c %s 2>&1 // Verify that function names starting with upper case are reported as invalid diff --git a/test/CodeStyleCheckerMacro.cpp b/test/CodeStyleCheckerMacro.cpp index 6966fd0..70cf12c 100644 --- a/test/CodeStyleCheckerMacro.cpp +++ b/test/CodeStyleCheckerMacro.cpp @@ -1,4 +1,6 @@ -// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -plugin CSC %s 2>&1 +// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -add-plugin CSC %s 2>&1 +// RUN: clang -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -c %s 2>&1 +// RUN: clang -fplugin=%shlibdir/libCodeStyleChecker%shlibext -c %s 2>&1 #define clang_tutor_class_ok(class_name) class ClangTutor##class_name #define clang_tutor_class_underscore(class_name) class Clang_TutorClass##class_name diff --git a/test/CodeStyleCheckerTypesAndVars.cpp b/test/CodeStyleCheckerTypesAndVars.cpp index b997d4f..8db3a9c 100644 --- a/test/CodeStyleCheckerTypesAndVars.cpp +++ b/test/CodeStyleCheckerTypesAndVars.cpp @@ -1,4 +1,6 @@ -// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -plugin CSC %s 2>&1 +// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -add-plugin CSC %s 2>&1 +// RUN: clang -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -c %s 2>&1 +// RUN: clang -fplugin=%shlibdir/libCodeStyleChecker%shlibext -c %s 2>&1 // Verify that type and variable names starting with lower case are reported as // invalid diff --git a/test/CodeStyleCheckerUnderscore.cpp b/test/CodeStyleCheckerUnderscore.cpp index 437107a..dbfa971 100644 --- a/test/CodeStyleCheckerUnderscore.cpp +++ b/test/CodeStyleCheckerUnderscore.cpp @@ -1,4 +1,6 @@ -// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -plugin CSC %s 2>&1 +// RUN: clang -cc1 -verify -load %shlibdir/libCodeStyleChecker%shlibext -add-plugin CSC %s 2>&1 +// RUN: clang -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -c %s 2>&1 +// RUN: clang -fplugin=%shlibdir/libCodeStyleChecker%shlibext -c %s 2>&1 // Verify that underscare in types, variables and function names are reported // as invalid diff --git a/test/CodeStyleCheckerVector.cpp b/test/CodeStyleCheckerVector.cpp index b297a9d..4590833 100644 --- a/test/CodeStyleCheckerVector.cpp +++ b/test/CodeStyleCheckerVector.cpp @@ -1,6 +1,6 @@ -// RUN: clang++ -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -plugin -Xclang CSC -c %s -// RUN: clang++ -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -plugin -Xclang CSC -Xclang -plugin-arg-CSC -Xclang -main-tu-only=true -c %s -// RUN: clang++ -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -plugin -Xclang CSC -Xclang -plugin-arg-CSC -Xclang -main-tu-only=false -c %s +// RUN: clang++ -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -c %s +// RUN: clang++ -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -Xclang -plugin-arg-CSC -Xclang -main-tu-only=true -c %s +// RUN: clang++ -Xclang -load -Xclang %shlibdir/libCodeStyleChecker%shlibext -Xclang -add-plugin -Xclang CSC -Xclang -plugin-arg-CSC -Xclang -main-tu-only=false -c %s #include diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index e33eed5..e1deeaa 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -40,4 +40,9 @@ foreach( tool ${CLANG_TUTOR_TOOLS} ) ${tool} "clangTooling" ) + + # ct action type should be CmdlineAfterMainAction + if(${tool} STREQUAL "ct-code-style-checker") + target_compile_definitions(${tool} PRIVATE TARGET_CLANG_TOOL=1) + endif() endforeach()