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

Savotina Valeria, Lab №1, var: 2. #27

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
93 changes: 93 additions & 0 deletions clang/labs/lab1/savotina_valeria/AddAttrAlwaysInline.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 AddAttrAlwaysInlineVisitor final
: public clang::RecursiveASTVisitor<AddAttrAlwaysInlineVisitor> {
public:
explicit AddAttrAlwaysInlineVisitor(clang::ASTContext *context_)
: context(context_) {}
bool VisitFunctionDecl(clang::FunctionDecl *decl) {
if (decl->isFunctionOrFunctionTemplate()) {
outStatusAttrAlwaysInline(decl);
if (!decl->hasAttr<clang::AlwaysInlineAttr>()) {
if (auto body = decl->getBody()) {
if (!findСonditionalStatement(body)) {
if (auto loc = decl->getSourceRange(); loc.isValid()) {
decl->addAttr(
clang::AlwaysInlineAttr::Create(*context, loc.getBegin()));
}
}
}
}
outStatusAttrAlwaysInline(decl);
llvm::outs() << "==================================\n";
}
return true;
}

private:
bool findСonditionalStatement(clang::Stmt *statement) {
if (!statement)
return false;

if (clang::isa<clang::IfStmt>(statement) ||
clang::isa<clang::SwitchStmt>(statement) ||
clang::isa<clang::DoStmt>(statement) ||
clang::isa<clang::WhileStmt>(statement) ||
clang::isa<clang::ForStmt>(statement))
return true;

if (auto parent = clang::dyn_cast<clang::CompoundStmt>(statement)) {
for (auto child : parent->body()) {
if (findСonditionalStatement(child))
return true;
}
}
return false;
}

void outStatusAttrAlwaysInline(clang::FunctionDecl *func) {
llvm::outs() << "function: " << func->getNameAsString() << '\n';
llvm::outs() << "attr status (always_inline): "
<< (func->hasAttr<clang::AlwaysInlineAttr>() ? "true\n"
: "false\n");
}

private:
clang::ASTContext *context;
};

class AddAttrAlwaysInlineConsumer final : public clang::ASTConsumer {
public:
explicit AddAttrAlwaysInlineConsumer(clang::ASTContext *сontext)
: visitor(сontext) {}

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

private:
AddAttrAlwaysInlineVisitor visitor;
};

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

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

static clang::FrontendPluginRegistry::Add<AddAttrAlwaysInlineAction>
X("add_attr_always_inline_plugin",
"Adds the always_inline attribute to functions without conditions");
14 changes: 14 additions & 0 deletions clang/labs/lab1/savotina_valeria/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_library(AddAttrAlwaysInlinePlugin MODULE AddAttrAlwaysInline.cpp PLUGIN_TOOL clang)

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

set(CLANG_TEST_DEPS "AddAttrAlwaysInlinePlugin" ${CLANG_TEST_DEPS} PARENT_SCOPE)
103 changes: 103 additions & 0 deletions clang/test/lab1/savotina_valeria/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// RUN: %clang_cc1 -load %llvmshlibdir/AddAttrAlwaysInlinePlugin%pluginext\
// RUN: -plugin add_attr_always_inline_plugin %s 1>&1 | FileCheck %s

namespace {
// CHECK: function: square
// CHECK: attr status (always_inline): false
// CHECK: function: square
// CHECK: attr status (always_inline): true
// CHECK: ==================================
int square(int value) { return value * value; }

// CHECK: function: diff
// CHECK: attr status (always_inline): false
// CHECK: function: diff
// CHECK: attr status (always_inline): true
// CHECK: ==================================
int diff(int valueOne, int valueTwo) {
{} {} {{} {}} {} {{{}} {} {}} {} {
{ return valueOne - valueTwo; }
}
}

// CHECK: function: max
// CHECK: attr status (always_inline): false
// CHECK: function: max
// CHECK: attr status (always_inline): true
// CHECK: ==================================
template <typename T> T max(T valueOne, T valueTwo) {
return valueOne > valueTwo ? valueOne : valueTwo;
}

// CHECK: function: emptyFunc
// CHECK: attr status (always_inline): true
// CHECK: function: emptyFunc
// CHECK: attr status (always_inline): true
// CHECK: ==================================
__attribute__((always_inline)) void emptyFunc() {
{} {{}} {{} {} {{{{}}}}} {}
}

// CHECK: function: funcTestIfStmt
// CHECK: attr status (always_inline): false
// CHECK: function: funcTestIfStmt
// CHECK: attr status (always_inline): false
// CHECK: ==================================
bool funcTestIfStmt(int value) {
if (value % 2)
return false;
return true;
}

// CHECK: function: funcTestSwitchStmt
// CHECK: attr status (always_inline): false
// CHECK: function: funcTestSwitchStmt
// CHECK: attr status (always_inline): false
// CHECK: ==================================
int funcTestSwitchStmt(int value) {
switch (value) {
case 1:
return value;
case 2:
return value;
case 3:
return value;
default:
return value;
}
}

// CHECK: function: funcTestWhileStmt
// CHECK: attr status (always_inline): false
// CHECK: function: funcTestWhileStmt
// CHECK: attr status (always_inline): false
// CHECK: ==================================
void funcTestWhileStmt(int value) {
{
while (value--) {
}
}
{} {}
}

// CHECK: function: funcTestDoStmt
// CHECK: attr status (always_inline): false
// CHECK: function: funcTestDoStmt
// CHECK: attr status (always_inline): false
// CHECK: ==================================
void funcTestDoStmt(int value) {
do {
--value;
} while (value);
}

// CHECK: function: funcTestForStmt
// CHECK: attr status (always_inline): false
// CHECK: function: funcTestForStmt
// CHECK: attr status (always_inline): false
// CHECK: ==================================
void funcTestForStmt(unsigned value) {
for (unsigned i = 0; i < value; ++i) {
}
}
} // namespace