Skip to content

Шишкарев Андрей. Лабораторная работа 1. Clang AST. Вариант 3. #43

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

Open
wants to merge 19 commits into
base: course-spring-2025
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set(Title "ImplicitConversionCounter")
set(Student "Shishkarev_Andrey")
set(Group "FIIT2")
set(TARGET_NAME "${Title}_${Student}_${Group}_ClangAST")

file(GLOB_RECURSE SOURCES *.cpp *.h *.hpp)
add_llvm_library(${TARGET_NAME} MODULE ${SOURCES} PLUGIN_TOOL clang)

if(WIN32 OR CYGWIN)
set(LLVM_LINK_COMPONENTS Support)
clang_target_link_libraries(${TARGET_NAME} PRIVATE
clangAST
clangBasic
clangFrontend
)
endif()

set(CLANG_TEST_DEPS "${TARGET_NAME}" ${CLANG_TEST_DEPS} PARENT_SCOPE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#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"

#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include <vector>

namespace {
class ImplicitConversionVisitor
: public clang::RecursiveASTVisitor<ImplicitConversionVisitor> {
public:
explicit ImplicitConversionVisitor(clang::ASTContext *Context)
: MContext(Context) {}

bool VisitFunctionDecl(clang::FunctionDecl *Func) {
llvm::outs() << "Function `" << Func->getName() << "`\n";
MConversions.clear();

if (Func->hasBody()) {
TraverseStmt(Func->getBody());
}

MConversions.erase(
std::remove_if(MConversions.begin(), MConversions.end(),
[](const std::pair<std::string, std::string> &Conv) {
return Conv.first == Conv.second;
}),
MConversions.end());

std::set<std::pair<std::string, std::string>> UniqueConversions;
MConversions.erase(
std::remove_if(MConversions.begin(), MConversions.end(),
[&UniqueConversions](
const std::pair<std::string, std::string> &Conv) {
return !UniqueConversions.insert(Conv).second;
}),
MConversions.end());

for (auto It = MConversions.rbegin(); It != MConversions.rend(); ++It) {
llvm::outs() << It->first << " -> " << It->second << ": 1\n";
}

return true;
}

bool VisitImplicitCastExpr(clang::ImplicitCastExpr *Expr) {
auto FromType = Expr->getSubExpr()->getType().getAsString();
auto ToType = Expr->getType().getAsString();

FromType = normalizeType(FromType, ToType);
ToType = normalizeType(ToType, FromType);

MConversions.emplace_back(FromType, ToType);

return RecursiveASTVisitor::VisitImplicitCastExpr(Expr);
}

private:
clang::ASTContext *MContext;
std::vector<std::pair<std::string, std::string>> MConversions;

std::string normalizeType(const std::string &Type,
const std::string &OtherType) {
std::string Normalized = Type;

size_t PtrPos = Normalized.find("(*)");
if (PtrPos != std::string::npos) {
Normalized.erase(PtrPos, 3); // Удаляем "(*)"
}
Comment on lines +72 to +74

Choose a reason for hiding this comment

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

Why is that needed?


std::string WithoutModifiers = Normalized;
WithoutModifiers.erase(
std::remove(WithoutModifiers.begin(), WithoutModifiers.end(), '&'),
WithoutModifiers.end());
WithoutModifiers.erase(
std::remove(WithoutModifiers.begin(), WithoutModifiers.end(), '*'),
WithoutModifiers.end());

if (WithoutModifiers == OtherType) {
Normalized = WithoutModifiers;
}

Normalized.erase(std::remove(Normalized.begin(), Normalized.end(), ' '),
Normalized.end());

return Normalized;
}
};

class ImplicitConversionConsumer : public clang::ASTConsumer {
public:
explicit ImplicitConversionConsumer(clang::ASTContext *Context)
: MVisitor(Context) {}

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

private:
ImplicitConversionVisitor MVisitor;
};

class ImplicitConversionAction : public clang::PluginASTAction {
public:
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &CI, llvm::StringRef) override {
return std::make_unique<ImplicitConversionConsumer>(&CI.getASTContext());
}

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

static clang::FrontendPluginRegistry::Add<ImplicitConversionAction>
X("implicit_conversion_plugin", "Counts implicit type conversions");
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %clang_cc1 -load %llvmshlibdir/ImplicitConversionCounter_Shishkarev_Andrey_FIIT2_ClangAST%pluginext -plugin implicit_conversion_plugin -fsyntax-only %s 2>&1 | FileCheck %s

// CHECK: Function `sum`
// CHECK-NEXT: int -> float: 1
// CHECK-NEXT: float -> double: 1

// CHECK: Function `mul`
// CHECK-NEXT: float -> int: 1
// CHECK-NEXT: float -> double: 1
// CHECK-NEXT: double -> int: 1

Choose a reason for hiding this comment

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

Please, extend these tests with your own ones

double sum(int a, float b) {
return a + b;
}

int mul(float a, float b) {
return a + sum(a, b);
Copy link

Choose a reason for hiding this comment

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

Suggested change
return a + sum(a, b);
return a + sum(a, (int)b);

}
Copy link

Choose a reason for hiding this comment

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

Add a new line at the end of file everywhere please.

Suggested change
}
}

Loading