From 99d76f6c33c34fd1fd2ec017a4b5f2167a3dff20 Mon Sep 17 00:00:00 2001 From: Chenhao Date: Tue, 4 Jun 2024 10:49:05 +0000 Subject: [PATCH 1/3] Adding the getActionType method to allow the clang plugin to automatically load plugins during the compilation process. --- README.md | 15 +++++++++++++++ lib/CodeStyleChecker.cpp | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 5d45906..25ed72c 100644 --- a/README.md +++ b/README.md @@ -358,6 +358,21 @@ the warnings with correct source code information. `-fcolor-diagnostics` above instructs Clang to generate color output (unfortunately Markdown doesn't render the colors here). +The **CodeStyleChecker** plugin could be used during the 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..2e97026 100644 --- a/lib/CodeStyleChecker.cpp +++ b/lib/CodeStyleChecker.cpp @@ -193,6 +193,10 @@ class CSCASTAction : public PluginASTAction { ros << "Help for CodeStyleChecker plugin goes here\n"; } + PluginASTAction::ActionType getActionType() override { + return AddAfterMainAction; + } + private: bool MainTuOnly = true; }; From cd932994ea9f16d1812e0a0b8f35a243f38f1c42 Mon Sep 17 00:00:00 2001 From: Chenhao Date: Wed, 5 Jun 2024 10:34:59 +0000 Subject: [PATCH 2/3] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 25ed72c..53f0688 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 From aac5d7eb3543817823d9b0d4fe1646f0513bf496 Mon Sep 17 00:00:00 2001 From: Chenhao Date: Wed, 5 Jun 2024 10:45:56 +0000 Subject: [PATCH 3/3] Make ClangPlugin **CodeStyleChecker** can be load automatically. --- README.md | 13 +++++++++++-- lib/CodeStyleChecker.cpp | 6 +++++- test/CodeStyleCheckerAnonymous.cpp | 4 +++- test/CodeStyleCheckerConversionOp.cpp | 4 +++- test/CodeStyleCheckerFunction.cpp | 4 +++- test/CodeStyleCheckerMacro.cpp | 4 +++- test/CodeStyleCheckerTypesAndVars.cpp | 4 +++- test/CodeStyleCheckerUnderscore.cpp | 4 +++- test/CodeStyleCheckerVector.cpp | 6 +++--- tools/CMakeLists.txt | 5 +++++ 10 files changed, 42 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 53f0688..96fdc24 100644 --- a/README.md +++ b/README.md @@ -358,8 +358,17 @@ the warnings with correct source code information. `-fcolor-diagnostics` above instructs Clang to generate color output (unfortunately Markdown doesn't render the colors here). -The **CodeStyleChecker** plugin could be used during the compilation process to -detect errors in the code, and get the output file, for example: +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 diff --git a/lib/CodeStyleChecker.cpp b/lib/CodeStyleChecker.cpp index 2e97026..17aea69 100644 --- a/lib/CodeStyleChecker.cpp +++ b/lib/CodeStyleChecker.cpp @@ -194,7 +194,11 @@ class CSCASTAction : public PluginASTAction { } PluginASTAction::ActionType getActionType() override { - return AddAfterMainAction; +#ifndef TARGET_CLANG_TOOL + return AddBeforeMainAction; +#else + return CmdlineAfterMainAction; +#endif } private: 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()