Skip to content

Commit

Permalink
Add a standalone tool that runs CodeStyleChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
banach-space committed Oct 13, 2020
1 parent 589f838 commit b40fc9e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 16 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ the warnings with correct source code information.
`-fcolor-diagnostics` above instructs Clang to generate color output
(unfortunately Markdown doesn't render the colors here).

### 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:

```bash
<build_dir>/bin/ct-code-style-checker input_file.cpp
```


## Obfuscator
The **Obfuscator** plugin will rewrite integer addition and subtraction
according to the following formulae:
Expand Down
21 changes: 21 additions & 0 deletions test/CodeStyleChecker-standalone-tool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: ../bin/ct-code-style-checker %s 2>&1 | FileCheck -implicit-check-not warning: %s

// This is a rather simplified test for CodeStyleChecker that focuses on the
// standalone wrapper tool: ct-code-style-checker.
//
// Unlike `clang -cc1`, ct-code-style-checker does not support `-verify` and we
// cannot re-use the tests implemented for the plugin (which do rely on
// `-verify`). Instead, this test is added as a small sanity-check. For proper
// testing of CodeStyleChecker see the other tests that use the plugin version.

// CHECK: CodeStyleChecker-standalone-tool.cpp:[[@LINE+1]]:6: warning: Function names should start with lower-case letter
void ClangTutorFuncBad();
void clangTutorFuncOK();

// CHECK: CodeStyleChecker-standalone-tool.cpp:[[@LINE+1]]:7: warning: Type and variable names should start with upper-case letter
class clangTutorClassBad;
class ClangTutorClassOK;

// CHECK: CodeStyleChecker-standalone-tool.cpp:[[@LINE+1]]:22: warning: `_` in names is not allowed
class ClangTutorClass_Bad;
class ClangTutorClassOK;
50 changes: 34 additions & 16 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
add_executable(lacommenter
# THE LIST OF TOOLS AND THE CORRESPONDING SOURCE FILES
# ====================================================
set(CLANG_TUTOR_TOOLS
lacommenter
ct-code-refactor
ct-code-style-checker
)

set(lacommenter_SOURCES
LACommenterMain.cpp
../lib/LACommenter.cpp
)

add_executable(ct-code-refactor
set(ct-code-refactor_SOURCES
CodeRefactorMain.cpp
../lib/CodeRefactor.cpp
)

target_link_libraries(lacommenter
clangTooling
)
set(ct-code-style-checker_SOURCES
CodeStyleCheckerMain.cpp
../lib/CodeStyleChecker.cpp
)

target_link_libraries(ct-code-refactor
clangTooling
)
# CONFIGURE THE TOOLS
# ===================
foreach( tool ${CLANG_TUTOR_TOOLS} )
# Create a library corresponding to 'plugin'
add_executable(
${tool}
${${tool}_SOURCES}
)

target_include_directories(
lacommenter
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../include")
# Link with libclangTooling
target_link_libraries(
${tool}
clangTooling
)

target_include_directories(
ct-code-refactor
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../include")
# Configure include directories for 'tool'
target_include_directories(
${tool}
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
)
endforeach()
51 changes: 51 additions & 0 deletions tools/CodeStyleCheckerMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//==============================================================================
// FILE:
// CodeStyleCheckerMain.cpp
//
// DESCRIPTION:
//
// USAGE:
//
// REFERENCES:
//
// License: The Unlicense
//==============================================================================
#include "CodeStyleChecker.h"

#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"

using namespace llvm;
using namespace clang;

//===----------------------------------------------------------------------===//
// Command line options
//===----------------------------------------------------------------------===//
static llvm::cl::OptionCategory CSCCategory("ct-code-style-checker options");

class CSCPluginAction : public PluginASTAction {
public:
bool ParseArgs(const CompilerInstance &CI,
const std::vector<std::string> &args) override {
return true;
}

std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef file) override {
return std::make_unique<CSCConsumer>(&CI.getASTContext());
}
};

//===----------------------------------------------------------------------===//
// Main driver code.
//===----------------------------------------------------------------------===//
int main(int Argc, const char **Argv) {
clang::tooling::CommonOptionsParser OptionsParser(Argc, Argv, CSCCategory);
clang::tooling::ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());

return Tool.run(
clang::tooling::newFrontendActionFactory<CSCPluginAction>().get());
}

0 comments on commit b40fc9e

Please sign in to comment.