-
Notifications
You must be signed in to change notification settings - Fork 54
Шишкарев Андрей. Лабораторная работа 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
base: course-spring-2025
Are you sure you want to change the base?
Changes from all commits
7947fb8
75a1542
c4584e0
7fdf953
ec7a66d
bedb23a
e441a4b
bc9fb19
00a6a8b
8edf163
a7f50a6
4e664c4
e54bf39
3622715
4aff031
ce07c33
6ed38e1
51af668
ee6354f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); // Удаляем "(*)" | ||
} | ||
|
||
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 | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is that needed?