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

Kozyreva Ekaterina. Lab 1. Option 1 #47

Merged
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
14 changes: 14 additions & 0 deletions clang/labs/lab1/kozyreva_ekaterina/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_library(kozyrevaPrintClassesNamePlugin MODULE PrintClassPlugin.cpp PLUGIN_TOOL clang)

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

set(CLANG_TEST_DEPS "kozyrevaPrintClassesNamePlugin" ${CLANG_TEST_DEPS} PARENT_SCOPE)
65 changes: 65 additions & 0 deletions clang/labs/lab1/kozyreva_ekaterina/PrintClassPlugin.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"

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

bool VisitCXXRecordDecl(clang::CXXRecordDecl *Res) {
llvm::outs() << Res->getNameAsString() << "\n";

for (clang::Decl *decl : Res->decls()) {
if (auto *field = clang::dyn_cast<clang::FieldDecl>(decl)) {
llvm::outs() << " |_ " << field->getNameAsString() << "\n";
} else if (auto *staticField = clang::dyn_cast<clang::VarDecl>(decl)) {
if (staticField->isStaticDataMember()) {
llvm::outs() << " |_ " << staticField->getNameAsString() << "\n";
}
}
}
return true;
}

private:
clang::ASTContext *Context;
};

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

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

private:
PrintClassesVisitor Visitor;
};

class PrintClassesPlugin : public clang::PluginASTAction {
public:
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &Compiler,
llvm::StringRef InFile) override {
return std::make_unique<PrintClassesConsumer>(&Compiler.getASTContext());
}

protected:
bool ParseArgs(const clang::CompilerInstance &Compiler,
const std::vector<std::string> &Args) override {
for (const std::string &arg : Args) {
if (arg == "--help") {
llvm::outs()
<< "This plugin displays the name of the class and it's fields.\n";
}
}
return true;
}
};

static clang::FrontendPluginRegistry::Add<PrintClassesPlugin>
X("print-classes", "Prints classes description.");
36 changes: 36 additions & 0 deletions clang/test/lab1/kozyreva_ekaterina/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %clang_cc1 -load %llvmshlibdir/kozyrevaPrintClassesNamePlugin%pluginext -plugin print-classes %s | FileCheck %s

// CHECK: Test1
struct Test1
{
// CHECK-NEXT: |_ A
int A;
// CHECK-NEXT: |_ B
int B;
};

// CHECK: Test2
class Test2
{
// CHECK-NEXT: |_ Arr
double Arr;
// CHECK-NEXT: |_ B
const int B = 2;
};

// CHECK: Test3
class Test3
{
// CHECK-NEXT: |_ Arg
static int Arg;
public:
// CHECK-NEXT: |_ Brr
int Brr = 2;
};

// CHECK: Test4
struct Test4{};

// RUN: %clang_cc1 -load %llvmshlibdir/kozyrevaPrintClassesNamePlugin%pluginext -plugin print-classes -plugin-arg-print-classes --help 1>&1 | FileCheck %s --check-prefix=HELP

// HELP: This plugin displays the name of the class and it's fields.
Loading