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

Kazantsev Evgeny. Lab 1, var 3. #40

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions clang/labs/lab1/kazantsev_evgeny/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_llvm_library(DeprecatedPlug MODULE DeprecatedPlug.cpp PLUGIN_TOOL clang)

if(WIN32 OR CYGWIN)
set(LLVM_LINK_COMPONENTS SUpport)
clang_target_link_libraries(DeprecatedPlug PRIVATE
clangAST
clangBasic
clangFrontend)
endif()

set(CLANG_TEST_DEPS "DeprecatedPlug" ${CLANG_TEST_DEPS} PARENT_SCOPE)
73 changes: 73 additions & 0 deletions clang/labs/lab1/kazantsev_evgeny/DeprecatedPlug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "llvm/Support/raw_ostream.h"

using namespace clang;

class DeprecatedFuncName : public RecursiveASTVisitor<DeprecatedFuncName> {
public:
explicit DeprecatedFuncName(
ASTContext
*Context) // get the AST tree context and initialize to "deprecated"
: Context(Context), DeprecatedIdentifier("deprecated") {}

bool VisitFunctionDecl(FunctionDecl *FD) {
// Check if the function name contains the word "deprecated"
std::string FuncName = FD->getNameAsString();
std::transform(FuncName.begin(), FuncName.end(), FuncName.begin(),
::tolower);
if (FuncName.find(DeprecatedIdentifier) != std::string::npos) {
// Emit a warning
SourceLocation Loc = FD->getLocation();
unsigned DiagID = Context->getDiagnostics().getCustomDiagID(
DiagnosticsEngine::Warning, "Deprecated function name");
Context->getDiagnostics().Report(Loc, DiagID);
}
return true;
}

private:
ASTContext *Context;
std::string DeprecatedIdentifier;
};

class DeprecatedConsumer : public ASTConsumer {
public:
explicit DeprecatedConsumer(ASTContext *Context) : Visitor(Context) {}

// traversal from the root node
void HandleTranslationUnit(ASTContext &Context) override {
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
}

private:
DeprecatedFuncName Visitor;
};

class DeprecatedFuncAction : public PluginASTAction {
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
llvm::StringRef) override {
return std::make_unique<DeprecatedConsumer>(&CI.getASTContext());
}
bool ParseArgs(const CompilerInstance &Compiler,
const std::vector<std::string> &args) override {
if (!args.empty() && args[0] == "help")
PrintHelp(llvm::errs());

return true;
}

void PrintHelp(llvm::raw_ostream &ros) {
ros << "Deprecated Plugin version 1.0\n";
}
};

// Register the plugin
static FrontendPluginRegistry::Add<DeprecatedFuncAction>
X("deprecated-function", "check for deprecated function names");
16 changes: 16 additions & 0 deletions clang/test/lab1/kazantsev_evgeny/testDep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %clang_cc1 -load %llvmshlibdir/DeprecatedPlug%pluginext -plugin deprecated-function %s 2>&1 | FileCheck %s --check-prefix=DEPRECATED
// REQUIRES: plugins

// DEPRECATED: warning: Deprecated function name
void Deprecated(int a, int b);

// DEPRECATED: warning: Deprecated function name
void dEpReCaTeD(int c);

// DEPRECATED-NOT: warning: Deprecated function name
void sqrt();

// RUN: %clang_cc1 -load %llvmshlibdir/DeprecatedPlug%pluginext -plugin deprecated-function %s -plugin-arg-deprecated-function help 2>&1 | FileCheck %s --check-prefix=HELP
// REQUIRES: plugins

// HELP: Deprecated Plugin version 1.0
Loading