|
| 1 | +#include "clang/AST/ASTConsumer.h" |
| 2 | +#include "clang/AST/RecursiveASTVisitor.h" |
| 3 | +#include "clang/Frontend/CompilerInstance.h" |
| 4 | +#include "clang/Frontend/FrontendPluginRegistry.h" |
| 5 | +#include "llvm/Support/raw_ostream.h" |
| 6 | + |
| 7 | +namespace { |
| 8 | +class AddAttrAlwaysInlineVisitor final |
| 9 | + : public clang::RecursiveASTVisitor<AddAttrAlwaysInlineVisitor> { |
| 10 | +public: |
| 11 | + explicit AddAttrAlwaysInlineVisitor(clang::ASTContext *context_) |
| 12 | + : context(context_) {} |
| 13 | + bool VisitFunctionDecl(clang::FunctionDecl *decl) { |
| 14 | + if (!decl->isFunctionOrFunctionTemplate()) |
| 15 | + return true; |
| 16 | + if (decl->hasAttr<clang::AlwaysInlineAttr>()) |
| 17 | + return true; |
| 18 | + if (auto body = decl->getBody()) { |
| 19 | + if (findСonditionalStatement(body)) |
| 20 | + return true; |
| 21 | + if (auto loc = decl->getSourceRange(); loc.isValid()) |
| 22 | + decl->addAttr( |
| 23 | + clang::AlwaysInlineAttr::Create(*context, loc.getBegin())); |
| 24 | + } |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | +private: |
| 29 | + bool findСonditionalStatement(clang::Stmt *statement) { |
| 30 | + if (!statement) |
| 31 | + return false; |
| 32 | + |
| 33 | + if (clang::isa<clang::IfStmt>(statement) || |
| 34 | + clang::isa<clang::SwitchStmt>(statement) || |
| 35 | + clang::isa<clang::DoStmt>(statement) || |
| 36 | + clang::isa<clang::WhileStmt>(statement) || |
| 37 | + clang::isa<clang::ForStmt>(statement)) |
| 38 | + return true; |
| 39 | + |
| 40 | + if (auto parent = clang::dyn_cast<clang::CompoundStmt>(statement)) { |
| 41 | + for (auto child : parent->body()) { |
| 42 | + if (findСonditionalStatement(child)) |
| 43 | + return true; |
| 44 | + } |
| 45 | + } |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + void outStatusAttrAlwaysInline(clang::FunctionDecl *func) { |
| 50 | + llvm::outs() << "function: " << func->getNameAsString() << '\n'; |
| 51 | + llvm::outs() << "attr status (always_inline): " |
| 52 | + << (func->hasAttr<clang::AlwaysInlineAttr>() ? "true\n" |
| 53 | + : "false\n"); |
| 54 | + } |
| 55 | + |
| 56 | +private: |
| 57 | + clang::ASTContext *context; |
| 58 | +}; |
| 59 | + |
| 60 | +class AddAttrAlwaysInlineConsumer final : public clang::ASTConsumer { |
| 61 | +public: |
| 62 | + explicit AddAttrAlwaysInlineConsumer(clang::ASTContext *сontext) |
| 63 | + : visitor(сontext) {} |
| 64 | + |
| 65 | + void HandleTranslationUnit(clang::ASTContext &context) override { |
| 66 | + visitor.TraverseDecl(context.getTranslationUnitDecl()); |
| 67 | + } |
| 68 | + |
| 69 | +private: |
| 70 | + AddAttrAlwaysInlineVisitor visitor; |
| 71 | +}; |
| 72 | + |
| 73 | +class AddAttrAlwaysInlineAction final : public clang::PluginASTAction { |
| 74 | +public: |
| 75 | + std::unique_ptr<clang::ASTConsumer> |
| 76 | + CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override { |
| 77 | + return std::make_unique<AddAttrAlwaysInlineConsumer>(&ci.getASTContext()); |
| 78 | + } |
| 79 | + |
| 80 | + bool ParseArgs(const clang::CompilerInstance &ci, |
| 81 | + const std::vector<std::string> &args) override { |
| 82 | + for (const auto &arg : args) { |
| 83 | + if (arg == "--help") { |
| 84 | + llvm::outs() << "Adds the always_inline attribute to functions without " |
| 85 | + "conditions\n"; |
| 86 | + } |
| 87 | + } |
| 88 | + return true; |
| 89 | + } |
| 90 | +}; |
| 91 | +} // namespace |
| 92 | + |
| 93 | +static clang::FrontendPluginRegistry::Add<AddAttrAlwaysInlineAction> |
| 94 | + X("add_attr_always_inline_plugin", |
| 95 | + "Adds the always_inline attribute to functions without conditions"); |
0 commit comments