Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit a9aa535

Browse files
aartyommm-ly4
andauthored
Kostin Artem. Lab #1. Var 3. (#15)
* warning plugin * fixed * add -i option * fix clang-tidy * fix CMakeLists * fix clang-tidy 2 --------- Co-authored-by: m-ly4 <[email protected]>
1 parent c4295cb commit a9aa535

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_llvm_library(deprWarnPlugin MODULE depr_plugin.cpp PLUGIN_TOOL clang)
2+
3+
if(WIN32 OR CYGWIN)
4+
set(LLVM_LINK_COMPONENTS
5+
Support
6+
)
7+
clang_target_link_libraries(deprWarnPlugin PRIVATE
8+
clangAST
9+
clangBasic
10+
clangFrontend
11+
)
12+
endif()
13+
14+
set(CLANG_TEST_DEPS "deprWarnPlugin" ${CLANG_TEST_DEPS} PARENT_SCOPE)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "clang/AST/ASTConsumer.h"
2+
#include "clang/AST/RecursiveASTVisitor.h"
3+
#include "clang/Frontend/CompilerInstance.h"
4+
#include "clang/Frontend/FrontendPluginRegistry.h"
5+
6+
using namespace clang;
7+
8+
class CustomNodeVisitor : public RecursiveASTVisitor<CustomNodeVisitor> {
9+
bool CaseInsensitive;
10+
11+
public:
12+
CustomNodeVisitor(bool CaseInsensitive) : CaseInsensitive(CaseInsensitive) {}
13+
bool VisitFunctionDecl(FunctionDecl *Pfunction) { // NOLINT
14+
std::string NameOfFunction = Pfunction->getNameInfo().getAsString();
15+
if (CaseInsensitive) {
16+
std::transform(NameOfFunction.begin(), NameOfFunction.end(),
17+
NameOfFunction.begin(), ::tolower);
18+
}
19+
if (NameOfFunction.find("deprecated") != std::string::npos) {
20+
DiagnosticsEngine &Diagnostics =
21+
Pfunction->getASTContext().getDiagnostics();
22+
unsigned int DiagnosticsId = Diagnostics.getCustomDiagID(
23+
DiagnosticsEngine::Warning,
24+
"The function name contains \"deprecated\"");
25+
SourceLocation PositionOfFunction = Pfunction->getLocation();
26+
Diagnostics.Report(PositionOfFunction, DiagnosticsId) << NameOfFunction;
27+
}
28+
return true;
29+
}
30+
};
31+
32+
class CustomConsumer : public ASTConsumer {
33+
bool CaseInsensitive;
34+
35+
public:
36+
explicit CustomConsumer(bool CaseInsensitive)
37+
: CaseInsensitive(CaseInsensitive) {}
38+
void HandleTranslationUnit(ASTContext &Context) override {
39+
CustomNodeVisitor Cnv(CaseInsensitive);
40+
Cnv.TraverseDecl(Context.getTranslationUnitDecl());
41+
}
42+
};
43+
44+
class PluginDeprFunc : public PluginASTAction {
45+
bool CaseInsensitive = false;
46+
std::unique_ptr<ASTConsumer>
47+
CreateASTConsumer(CompilerInstance &Instance,
48+
llvm::StringRef InFile) override {
49+
return std::make_unique<CustomConsumer>(CaseInsensitive);
50+
}
51+
bool ParseArgs(const CompilerInstance &Compiler,
52+
const std::vector<std::string> &Args) override {
53+
for (const auto &arg : Args) {
54+
if (arg == "-i") {
55+
CaseInsensitive = true;
56+
}
57+
}
58+
return true;
59+
}
60+
};
61+
62+
static FrontendPluginRegistry::Add<PluginDeprFunc>
63+
X("plugin_for_deprecated_functions",
64+
"If the function name contains \"deprecated\" plugin writes a warning");
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: split-file %s %t
2+
// RUN: %clang_cc1 -load %llvmshlibdir/deprWarnPlugin%pluginext -plugin plugin_for_deprecated_functions %t/with_-i.cpp -plugin-arg-plugin_for_deprecated_functions -i 2>&1 | FileCheck %t/with_-i.cpp
3+
// RUN: %clang_cc1 -load %llvmshlibdir/deprWarnPlugin%pluginext -plugin plugin_for_deprecated_functions %t/without_-i.cpp 2>&1 | FileCheck %t/without_-i.cpp
4+
5+
6+
//--- with_-i.cpp
7+
8+
// CHECK: warning: The function name contains "deprecated"
9+
void deprecatedFunction();
10+
11+
// CHECK: warning: The function name contains "deprecated"
12+
void functionWithDeprecatedWord();
13+
14+
// CHECK-NOT: warning: The function name contains "deprecated"
15+
void regularFunction();
16+
17+
class SomeClass {
18+
// CHECK: warning: The function name contains "deprecated"
19+
void functionWithDePrEcAtEdWord();
20+
// CHECK-NOT: warning: The function name contains "deprecated"
21+
void regularFunctionAgain();
22+
};
23+
24+
//--- without_-i.cpp
25+
26+
// CHECK: warning: The function name contains "deprecated"
27+
void deprecatedFunction2();
28+
29+
// CHECK-NOT: warning: The function name contains "deprecated"
30+
void functionWithDeprecatedWord2();
31+
32+
// CHECK-NOT: warning: The function name contains "deprecated"
33+
void regularFunction2();
34+
35+
class SomeClass2 {
36+
// CHECK-NOT: warning: The function name contains "deprecated"
37+
void functionWithDePrEcAtEdWord2();
38+
// CHECK-NOT: warning: The function name contains "deprecated"
39+
void regularFunctionAgain2();
40+
};

0 commit comments

Comments
 (0)