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

Kurdina Julia. Lab 1. Var 3 #65

Merged
merged 10 commits into from
May 12, 2024
Merged
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
62 changes: 62 additions & 0 deletions clang/labs/lab1/kurdina_julia/AddWarning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"

using namespace clang;

class AddWarningConsumer : public ASTConsumer {
CompilerInstance &Instance;
bool withoutClass;

public:
AddWarningConsumer(CompilerInstance &Instance, bool withoutClass)
: Instance(Instance), withoutClass(withoutClass) {}

void HandleTranslationUnit(ASTContext &context) override {

struct Visitor : public RecursiveASTVisitor<Visitor> {
ASTContext *context;
bool withoutClass;
Visitor(ASTContext *context, bool withoutClass)
: context(context), withoutClass(withoutClass) {}

bool VisitFunctionDecl(FunctionDecl *FD) {
if (!withoutClass || !FD->isCXXClassMember()) {
std::string name = FD->getNameInfo().getAsString();
if (name.find("deprecated") != std::string::npos) {
DiagnosticsEngine &diag = context->getDiagnostics();
unsigned diagID = diag.getCustomDiagID(
DiagnosticsEngine::Warning, "Function or method is deprecated");
SourceLocation location = FD->getLocation();
diag.Report(location, diagID);
}
}
return true;
}
} v(&Instance.getASTContext(), withoutClass);

v.TraverseDecl(context.getTranslationUnitDecl());
}
};

class AddWarningAction : public PluginASTAction {
protected:
bool withoutClass = false;
std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
return std::make_unique<AddWarningConsumer>(CI, withoutClass);
}

bool ParseArgs(const CompilerInstance &CI,
const std::vector<std::string> &args) override {
for (const auto &arg : args) {
if (arg == "-notCheckClass") {
withoutClass = true;
}
}
return true;
}
};

static FrontendPluginRegistry::Add<AddWarningAction> X("warn_dep", "warn_dep");
14 changes: 14 additions & 0 deletions clang/labs/lab1/kurdina_julia/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_library(addWarning MODULE AddWarning.cpp PLUGIN_TOOL clang)

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

set(CLANG_TEST_DEPS "addWarning" ${CLANG_TEST_DEPS} PARENT_SCOPE)
45 changes: 45 additions & 0 deletions clang/test/lab1/kurdina_julia/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: split-file %s %t
// RUN: %clang_cc1 -load %llvmshlibdir/addWarning%pluginext -plugin warn_dep %t/with_notCheckClass.cpp -plugin-arg-warn_dep -notCheckClass 2>&1 | FileCheck %t/with_notCheckClass.cpp
// RUN: %clang_cc1 -load %llvmshlibdir/addWarning%pluginext -plugin warn_dep %t/without_notCheckClass.cpp 2>&1 | FileCheck %t/without_notCheckClass.cpp

//--- with_notCheckClass.cpp

// CHECK: warning: Function or method is deprecated
void deprecated();

// CHECK: warning: Function or method is deprecated
void function_name_is_deprecated();

// CHECK-NOT: warning: Function or method is deprecated
void function();

// CHECK-NOT: warning: Function or method is deprecated
void function_depr();

class CheckClass {
// CHECK-NOT: warning: Function or method is deprecated
void deprecated();
// CHECK-NOT: warning: Function or method is deprecated
void function();
};

//--- without_notCheckClass.cpp

// CHECK: warning: Function or method is deprecated
void deprecated();

// CHECK: warning: Function or method is deprecated
void function_name_is_deprecated();

// CHECK-NOT: warning: Function or method is deprecated
void function();

// CHECK-NOT: warning: Function or method is deprecated
void function_depr();

class CheckClass {
// CHECK: warning: Function or method is deprecated
void deprecated();
// CHECK-NOT: warning: Function or method is deprecated
void function();
};
Loading