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

Veselov Ilya lab 1 var 1 #36

Merged
merged 13 commits into from
Mar 21, 2024
14 changes: 14 additions & 0 deletions clang/labs/lab1/veselov_ilya/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_library(PrintClassDescPlugin MODULE printClass.cpp PLUGIN_TOOL clang)

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

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

class PrintClassVisitor : public clang::RecursiveASTVisitor<PrintClassVisitor> {
public:
bool VisitCXXRecordDecl(clang::CXXRecordDecl *decl) {
if (decl->isStruct() || decl->isClass()) {
llvm::outs() << decl->getNameAsString() << "\n";
for (auto field : decl->fields()) {
llvm::outs() << "|_" << field->getNameAsString() << "\n";
}
llvm::outs() << "\n";
}
return true;
}
};

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

private:
PrintClassVisitor Visitor;
};

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

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

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

// CHECK: Empty
class Empty {};

// CHECK: MyStruct
struct MyStruct{
// CHECK-NEXT: |_a
int a;
// CHECK-NEXT: |_b
int b;
};

// CHECK: MyClass
class MyClass {
// CHECK-NEXT: |_a_c
int a_c;
// CHECK-NEXT: |_b_c
float b_c;
// CHECK-NEXT: |_c_c
double c_c;
};

// CHECK: outerClass
class outerClass {
// CHECK: innerClass
class innerClass {
//CHECK-NEXT: |_var
float var;
};
};

// CHECK: TemplateClass
template<typename T> class TemplateClass {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also add test with instantiation of this class for a concrete type?

//CHECK-NEXT: Tvariable
T Tvariable;
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// CHECK: TemplateClass
//CHECK-NEXT: Tvariable


TemplateClass<int> instantiationInt;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please print instantiated class and its fields?


// CHECK: StaticClass
class StaticClass {
// CHECK-NEXT: |_staticField
static int staticField;
};
Loading