This repository was archived by the owner on Jan 19, 2025. It is now read-only.
forked from NN-complr-tech/llvm
-
Notifications
You must be signed in to change notification settings - Fork 57
Savotina Valeria, Lab №1, var: 2. #27
Merged
m-ly4
merged 5 commits into
NN-complr-tech:course-spring-2024
from
Savotina:savotina_valeria
Mar 21, 2024
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
m-ly4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}; | ||
} // namespace | ||
|
||
static clang::FrontendPluginRegistry::Add<AddAttrAlwaysInlineAction> | ||
X("add_attr_always_inline_plugin", | ||
"Adds the always_inline attribute to functions without conditions"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// RUN: %clang_cc1 -load %llvmshlibdir/AddAttrAlwaysInlinePlugin%pluginext\ | ||
m-ly4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// RUN: -plugin add_attr_always_inline_plugin %s 1>&1 | FileCheck %s | ||
m-ly4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.