Skip to content

Petrov Maksim. Lab 1. Var 3. #4

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

Closed
4 changes: 3 additions & 1 deletion .github/workflows/nn-cmplr-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install clang-format
run: |
sudo apt-get install -y clang-format
- name: Run clang-format
run: |
git-clang-format --diff `git merge-base ${GITHUB_SHA} ${GITHUB_BASE_REF}`
git-clang-format --diff `git merge-base ${GITHUB_SHA} origin/${GITHUB_BASE_REF}` ${GITHUB_SHA}
clang-tidy:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ add_subdirectory(runtime)

option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
add_subdirectory(examples)
add_subdirectory(labs)

if(APPLE)
# this line is needed as a cleanup to ensure that any CMakeCaches with the old
Expand Down
9 changes: 9 additions & 0 deletions clang/labs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FILE(GLOB children RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*)
FOREACH(child ${children})
IF(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${child})
add_subdirectory(${child})
ENDIF()
ENDFOREACH()

set(CLANG_TEST_DEPS ${CLANG_TEST_DEPS} PARENT_SCOPE)
message("CLANG_TEST_DEPS ${CLANG_TEST_DEPS}")
10 changes: 10 additions & 0 deletions clang/labs/lab1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if(CLANG_PLUGIN_SUPPORT)
FILE(GLOB children RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*)
FOREACH(child ${children})
IF(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${child})
add_subdirectory(${child})
ENDIF()
ENDFOREACH()

set(CLANG_TEST_DEPS ${CLANG_TEST_DEPS} PARENT_SCOPE)
endif()
14 changes: 14 additions & 0 deletions clang/labs/lab1/ivanov_nikita/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_library(clangDepMatcher MODULE deprecatedMatcher.cpp PLUGIN_TOOL clang)

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

set(CLANG_TEST_DEPS "clangDepMatcher" ${CLANG_TEST_DEPS} PARENT_SCOPE)
59 changes: 59 additions & 0 deletions clang/labs/lab1/ivanov_nikita/deprecatedMatcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"

using namespace clang;

class DepConsumer : public ASTConsumer {
CompilerInstance &Instance;

public:
explicit DepConsumer(CompilerInstance &CI) : Instance(CI) {}

void HandleTranslationUnit(ASTContext &Context) override {
struct Visitor : public RecursiveASTVisitor<Visitor> {
ASTContext *Context;

Visitor(ASTContext *Context) : Context(Context) {}

bool VisitFunctionDecl(FunctionDecl *Func) {
if (Func->getNameInfo().getAsString().find("deprecated") !=
std::string::npos) {
DiagnosticsEngine &Diags = Context->getDiagnostics();
unsigned DiagID = Diags.getCustomDiagID(
DiagnosticsEngine::Warning, "Deprecated in function name");
Diags.Report(Func->getLocation(), DiagID)
<< Func->getNameInfo().getAsString();
}
return true;
}
} v(&Instance.getASTContext());

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

class DeprecatedPlugin : public PluginASTAction {
protected:
std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance &Compiler,
llvm::StringRef InFile) override {
return std::make_unique<DepConsumer>(Compiler);
}

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";
}
};

static FrontendPluginRegistry::Add<DeprecatedPlugin> X("deprecated-match",
"deprecated match");
14 changes: 14 additions & 0 deletions clang/labs/lab1/kulikov_artem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_library(DeprecatedWarningPlugin MODULE DeprecatedWarning.cpp PLUGIN_TOOL clang)

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

set(CLANG_TEST_DEPS "DeprecatedWarningPlugin" ${CLANG_TEST_DEPS} PARENT_SCOPE)
54 changes: 54 additions & 0 deletions clang/labs/lab1/kulikov_artem/DeprecatedWarning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"

using namespace clang;

class WarnVisitor : public RecursiveASTVisitor<WarnVisitor> {
public:
explicit WarnVisitor(ASTContext *Context) : Context(Context) {}

bool VisitFunctionDecl(FunctionDecl *Func) {
const char *name_data = Func->getName().data();
if (std::string(name_data).find("deprecated") != std::string::npos) {
DiagnosticsEngine &Diags = Context->getDiagnostics();
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
"Function '%0' contains 'deprecated' in its name");
Diags.Report(Func->getLocation(), DiagID) << name_data;
}
return true;
}

private:
ASTContext *Context;
};

class WarnConsumer : public ASTConsumer {
public:
explicit WarnConsumer(CompilerInstance &CI) : Visitor(&CI.getASTContext()) {}

void HandleTranslationUnit(ASTContext &Context) override {
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
}

private:
WarnVisitor Visitor;
};

class WarnPlugin : public clang::PluginASTAction {
public:
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer (
clang::CompilerInstance &Compiler, llvm::StringRef InFile) override {
return std::unique_ptr<clang::ASTConsumer>(new WarnConsumer(Compiler));
}

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

static FrontendPluginRegistry::Add<WarnPlugin>
X("warn-deprecated", "Prints a warning if a function name contains 'deprecated'");
14 changes: 14 additions & 0 deletions clang/labs/lab1/kuznetsov_artyom/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_library(PrintClassMembersPlugin MODULE PrintClassMembers.cpp PLUGIN_TOOL clang)

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

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

namespace {
class PrintClassMembersVisitor final
: public clang::RecursiveASTVisitor<PrintClassMembersVisitor> {
public:
explicit PrintClassMembersVisitor(clang::ASTContext *context)
: m_context(context) {}
bool VisitCXXRecordDecl(clang::CXXRecordDecl *declaration) {
if (declaration->isStruct() || declaration->isClass()) {
outInfoUserType(declaration);

for (const auto &decl : declaration->decls()) {
if (auto notStaticMember = llvm::dyn_cast<clang::FieldDecl>(decl)) {
llvm::outs() << "|_ " << notStaticMember->getNameAsString() << ' ';
llvm::outs() << '(' << getTypeAsString(notStaticMember) << '|';
llvm::outs() << getAccessSpecifierAsString(notStaticMember) << ")\n";
} else if (auto staticMember = llvm::dyn_cast<clang::VarDecl>(decl)) {
if (staticMember->isStaticDataMember()) {
llvm::outs() << "|_ " << staticMember->getNameAsString() << ' ';
llvm::outs() << '(' << getTypeAsString(staticMember) << '|';
llvm::outs() << getAccessSpecifierAsString(staticMember)
<< "|static)\n";
}
}
}
llvm::outs() << '\n';
}
return true;
}

private:
void outInfoUserType(clang::CXXRecordDecl *userType) {
llvm::outs() << userType->getNameAsString() << ' ';
llvm::outs() << (userType->isStruct() ? "(struct" : "(class");
llvm::outs() << (userType->isTemplated() ? "|template)" : ")") << '\n';
}

std::string getTypeAsString(const clang::ValueDecl *member) {
clang::QualType type = member->getType();
return type.getAsString();
}

std::string getAccessSpecifierAsString(const clang::ValueDecl *member) {
switch (member->getAccess()) {
case clang::AS_public:
return "public";
case clang::AS_protected:
return "protected";
case clang::AS_private:
return "private";
default:
return "unknown";
}
}

private:
clang::ASTContext *m_context;
};

class PrintClassMembersConsumer final : public clang::ASTConsumer {
public:
explicit PrintClassMembersConsumer(clang::ASTContext *сontext)
: m_visitor(сontext) {}

void HandleTranslationUnit(clang::ASTContext &context) override {
m_visitor.TraverseDecl(context.getTranslationUnitDecl());
}

private:
PrintClassMembersVisitor m_visitor;
};

class PrintClassMembersAction final : public clang::PluginASTAction {
public:
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override {
return std::make_unique<PrintClassMembersConsumer>(&ci.getASTContext());
}

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

static clang::FrontendPluginRegistry::Add<PrintClassMembersAction>
X("pcm_plugin", "Prints all members of the class");
14 changes: 14 additions & 0 deletions clang/labs/lab1/petrov_maksim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_library(WarningDeprecatedPlugin MODULE WarningDeprecatedPlugin.cpp PLUGIN_TOOL clang)

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

set(CLANG_TEST_DEPS "WarningDeprecatedPlugin" ${CLANG_TEST_DEPS} PARENT_SCOPE)
65 changes: 65 additions & 0 deletions clang/labs/lab1/petrov_maksim/WarningDeprecatedPlugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"

using namespace clang;

class WarningDepricatedVisitor
: public RecursiveASTVisitor<WarningDepricatedVisitor> {
private:
ASTContext *Context;

public:
explicit WarningDepricatedVisitor(ASTContext *Context) {this->Context = Context;}

bool VisitFunctionDecl(FunctionDecl *F) {

std::string FuncName = F->getNameInfo().getAsString();
const std::string DeprecatedKeyword = "deprecated";

if ((FuncName.find(DeprecatedKeyword) != std::string::npos)) {

DiagnosticsEngine &Diagnostics = Context->getDiagnostics();
unsigned warningID = Diagnostics.getCustomDiagID(
DiagnosticsEngine::Warning,
"Function '%0' contains 'deprecated' in its name");

Diagnostics.Report(F->getLocation(), warningID) << FuncName;
}
return true;
}
};

class WarningDepricatedConsumer : public ASTConsumer {
private:
WarningDepricatedVisitor Visitor;

public:
explicit WarningDepricatedConsumer(CompilerInstance &CI)
: Visitor(&CI.getASTContext()) {}

void HandleTranslationUnit(ASTContext &Context) override {
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
}
};

class WarningDeprecatedPlugin : public clang::PluginASTAction {
public:
std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance &Compiler,
llvm::StringRef InFile) override {
return std::unique_ptr<ASTConsumer>(
new WarningDepricatedConsumer(Compiler));
}

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

static FrontendPluginRegistry::Add<WarningDeprecatedPlugin>
X("WarningDeprecatedPlugin",
"Outputs a warning if there is a function with 'deprecated' in its name");
Loading