Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kudasov Maxim, 21ami2, lab1, 3rd variant #7

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,5 @@ set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
configure_file(
${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
${CLANG_BINARY_DIR}/include/clang/Config/config.h)

add_subdirectory(lab1/Kudasov_Maxim)
17 changes: 17 additions & 0 deletions clang/lab1/Kudasov_Maxim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_llvm_library(KickDeprecated MODULE KickDeprecated.cpp
PLUGIN_TOOL clang)

if(WIN32 OR CYGWIN)
set(LLVM_LINK_COMPONENTS Support)
clang_target_link_libraries(KickDeprecated PRIVATE
clangAST
clangBasic
clangFrontend
llvmSupport
)
endif()

set_target_properties(KickDeprecated
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
69 changes: 69 additions & 0 deletions clang/lab1/Kudasov_Maxim/KickDeprecated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "llvm/Support/raw_ostream.h"

class FindDeprecatedFuncDeclVisitor
: public clang::RecursiveASTVisitor<FindDeprecatedFuncDeclVisitor> {
public:
FindDeprecatedFuncDeclVisitor(
clang::ASTContext& Context,
clang::CompilerInstance& Compiler
) : context(Context),
compiler(Compiler),
engine(Compiler.getDiagnostics()) {}

bool VisitFunctionDecl(clang::FunctionDecl *Declaration) {
if (Declaration->getName().contains("deprecated")) {
clang::SourceLocation loc = Declaration->getLocation();
unsigned int warningId = engine.getCustomDiagID(
clang::DiagnosticsEngine::Warning,
"This function-imposter contains \'deprecated\': \'%0\'"
);
engine.Report(loc, warningId) << Declaration->getNameAsString();
}
return true;
}
private:
clang::ASTContext& context;
clang::CompilerInstance& compiler;
clang::DiagnosticsEngine& engine;
};

class KickDeprecatedWarnConsumer : public clang::ASTConsumer {
public:
KickDeprecatedWarnConsumer(clang::CompilerInstance& Compiler)
: compiler(Compiler), visitor(Compiler.getASTContext(), Compiler) {}

virtual void HandleTranslationUnit(clang::ASTContext& Context) override {
visitor.TraverseDecl(Context.getTranslationUnitDecl());
}
private:
clang::CompilerInstance& compiler;
FindDeprecatedFuncDeclVisitor visitor;
};

class KickDeprecatedWarnAction : public clang::PluginASTAction {
protected:
virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
clang::CompilerInstance &Compiler,
llvm::StringRef
) override {
return std::make_unique<KickDeprecatedWarnConsumer>(Compiler);
}

bool ParseArgs(
const clang::CompilerInstance &Compiler,
const std::vector<std::string> &args
) override {
return true;
}
};

static clang::FrontendPluginRegistry::Add<KickDeprecatedWarnAction>
X("kick_deprecated_with_warn",
"Pop up warning when clang detects function containing \'deprecated\' in name"
);
Binary file added clang/lab1/Kudasov_Maxim/KickDeprecated.so
Binary file not shown.
35 changes: 35 additions & 0 deletions clang/test/lab1/Kudasov_Maxim/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %clang++ -cc1 -load %S/../../../lab1/Kudasov_Maxim/KickDeprecated.so -plugin kick_deprecated_with_warn %s 2>&1 | FileCheck %s

// CHECK: warning: This function-imposter contains 'deprecated': 'deprecatedFoo'
void deprecatedFoo() { return; }

// CHECK: warning: This function-imposter contains 'deprecated': 'deprecatedDeprecated_deprecated'
float deprecatedDeprecated_deprecated() { return 1488.0; }

// CHECK: warning: This function-imposter contains 'deprecated': 'myDeprecatedFunc_deprecated'
void myDeprecatedFunc_deprecated() { return; }

// CHECK-NOT: warning: This function-imposter contains 'deprecated': 'fooBar'
float fooBar() { return 42.0; }

// CHECK-NOT: warning: This function-imposter contains 'deprecated': 'derprecated_createArray'
int* derprecated_createArray() {
int* newArray = new int[5];
return newArray;
}

// CHECK-NOT: warning: This function-imposter contains 'deprecated': 'myDeprecatedFunc'
void myDeprecatedFunc() { return; }

// CHECK-NOT: warning: This function-imposter contains 'deprecated': 'myDeprecatedVariable'
float myDeprecatedVariable = 148.8;

// CHECK-NOT: warning: This function-imposter contains 'deprecated': 'deprecatedVariable'
float deprecatedVariable = 11.9;

// CHECK-NOT: warning: This function-imposter contains 'deprecated': 'variable_deprecated'
float variable_deprecated = 148.8;

// CHECK-NOT: warning: This function-imposter contains 'deprecated': 'class_deprecated'
class class_deprecated{};