Skip to content

Commit e93edd2

Browse files
committed
nit: clean up
1 parent dad508e commit e93edd2

File tree

4 files changed

+33
-74
lines changed

4 files changed

+33
-74
lines changed

tools/clang-tidy-plugin/GeneralsGameCodeTidyModule.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,9 @@ void GeneralsGameCodeTidyModule::addCheckFactories(
1919

2020
} // namespace clang::tidy::generalsgamecode
2121

22-
// Static registration using LLVM's registry system
23-
// This is the "hack" mentioned in the documentation for out-of-tree plugins
24-
// The static initializer should run when the library is loaded
2522
static llvm::Registry<::clang::tidy::ClangTidyModule>::Add<
2623
::clang::tidy::generalsgamecode::GeneralsGameCodeTidyModule>
2724
X("generalsgamecode", "GeneralsGameCode-specific checks");
2825

29-
// Anchor variable to force linker to include this file
30-
// This ensures the static initializer above actually runs
3126
volatile int GeneralsGameCodeTidyModuleAnchorSource = 0;
3227

tools/clang-tidy-plugin/README.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,57 @@ if (!str.isEmpty()) { ... }
2222
2323
## Building
2424
25-
This plugin requires LLVM and Clang to be installed. You can build it with CMake:
25+
This plugin requires LLVM and Clang to be installed. Build it with CMake:
2626
2727
```bash
2828
cd tools/clang-tidy-plugin
29-
mkdir build
30-
cd build
29+
mkdir build && cd build
3130
cmake .. -DLLVM_DIR=/path/to/llvm/lib/cmake/llvm -DClang_DIR=/path/to/clang/lib/cmake/clang
3231
make
3332
```
3433

3534
The plugin will be built as a shared library (`.so` on Linux, `.dylib` on macOS, `.dll` on Windows) in the `build/lib/` directory.
3635

36+
## Prerequisites
37+
38+
Before using the plugin, you need to generate a compile commands database:
39+
40+
```bash
41+
cmake -B build/clang-tidy -DCMAKE_DISABLE_PRECOMPILE_HEADERS=ON -G Ninja
42+
```
43+
44+
This creates `build/clang-tidy/compile_commands.json` which tells clang-tidy how to compile each file.
45+
3746
## Usage
3847

39-
Load the plugin when running clang-tidy:
48+
### Basic Usage
4049

4150
```bash
42-
clang-tidy --checks=-*,generals-use-is-empty \
43-
-load build/lib/GeneralsGameCodeClangTidyPlugin.so \
44-
source.cpp
51+
clang-tidy -p build/clang-tidy \
52+
--checks='-*,generals-use-is-empty' \
53+
-load tools/clang-tidy-plugin/build/lib/libGeneralsGameCodeClangTidyPlugin.so \
54+
file.cpp
4555
```
4656

47-
Or add it to your `.clang-tidy` configuration file:
57+
### With Automatic Fixes
4858

49-
```yaml
50-
Checks: '-*,generals-use-is-empty'
59+
```bash
60+
clang-tidy -p build/clang-tidy \
61+
--checks='-*,generals-use-is-empty' \
62+
-load tools/clang-tidy-plugin/build/lib/libGeneralsGameCodeClangTidyPlugin.so \
63+
-fix-errors \
64+
file.cpp
5165
```
5266

53-
And load it with:
67+
### On Multiple Files
5468

5569
```bash
56-
clang-tidy -load build/lib/GeneralsGameCodeClangTidyPlugin.so source.cpp
70+
find Core -name "*.cpp" -type f -exec clang-tidy \
71+
-p build/clang-tidy \
72+
--checks='-*,generals-use-is-empty' \
73+
-load tools/clang-tidy-plugin/build/lib/libGeneralsGameCodeClangTidyPlugin.so \
74+
-fix-errors {} \;
5775
```
5876

77+
The `-p build/clang-tidy` flag tells clang-tidy to use the compile commands database to understand how to parse each file.
78+

tools/clang-tidy-plugin/readability/UseIsEmptyCheck.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ using namespace clang::ast_matchers;
1919
namespace clang::tidy::generalsgamecode::readability {
2020

2121
void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
22-
// Match member calls to getLength() on AsciiString or UnicodeString
23-
// followed by comparison with 0
2422
auto GetLengthCall = cxxMemberCallExpr(
2523
callee(cxxMethodDecl(hasName("getLength"))),
2624
on(hasType(hasUnqualifiedDesugaredType(
2725
recordType(hasDeclaration(cxxRecordDecl(
2826
hasAnyName("AsciiString", "UnicodeString"))))))));
2927

30-
// Match: obj.getLength() == 0
3128
Finder->addMatcher(
3229
binaryOperator(
3330
hasOperatorName("=="),
@@ -36,7 +33,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
3633
.bind("comparison"),
3734
this);
3835

39-
// Match: obj.getLength() != 0
4036
Finder->addMatcher(
4137
binaryOperator(
4238
hasOperatorName("!="),
@@ -45,7 +41,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
4541
.bind("comparison"),
4642
this);
4743

48-
// Match: obj.getLength() > 0
4944
Finder->addMatcher(
5045
binaryOperator(
5146
hasOperatorName(">"),
@@ -54,7 +49,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
5449
.bind("comparison"),
5550
this);
5651

57-
// Match: obj.getLength() <= 0
5852
Finder->addMatcher(
5953
binaryOperator(
6054
hasOperatorName("<="),
@@ -63,7 +57,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
6357
.bind("comparison"),
6458
this);
6559

66-
// Match: 0 == obj.getLength()
6760
Finder->addMatcher(
6861
binaryOperator(
6962
hasOperatorName("=="),
@@ -72,7 +65,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
7265
.bind("comparison"),
7366
this);
7467

75-
// Match: 0 != obj.getLength()
7668
Finder->addMatcher(
7769
binaryOperator(
7870
hasOperatorName("!="),
@@ -90,12 +82,10 @@ void UseIsEmptyCheck::check(const MatchFinder::MatchResult &Result) {
9082
if (!Comparison || !GetLengthCall)
9183
return;
9284

93-
// Get the object on which getLength() is called
9485
const Expr *ObjectExpr = GetLengthCall->getImplicitObjectArgument();
9586
if (!ObjectExpr)
9687
return;
9788

98-
// Determine the replacement based on the operator
9989
StringRef Operator = Comparison->getOpcodeStr();
10090
bool ShouldNegate = false;
10191

@@ -108,23 +98,20 @@ void UseIsEmptyCheck::check(const MatchFinder::MatchResult &Result) {
10898
} else if (Operator == "<=") {
10999
ShouldNegate = false;
110100
} else {
111-
return; // Unsupported operator
101+
return;
112102
}
113103

114-
// Get the source text for the object expression
115104
StringRef ObjectText = Lexer::getSourceText(
116105
CharSourceRange::getTokenRange(ObjectExpr->getSourceRange()),
117106
*Result.SourceManager, Result.Context->getLangOpts());
118107

119-
// Build the replacement text
120108
std::string Replacement;
121109
if (ShouldNegate) {
122110
Replacement = "!" + ObjectText.str() + ".isEmpty()";
123111
} else {
124112
Replacement = ObjectText.str() + ".isEmpty()";
125113
}
126114

127-
// Create the fix - replace the entire comparison
128115
SourceLocation StartLoc = Comparison->getBeginLoc();
129116
SourceLocation EndLoc = Comparison->getEndLoc();
130117

tools/clang-tidy-plugin/test_file.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)