File indexing completed on 2024-04-14 05:32:07

0001 /*
0002     SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com
0003     SPDX-FileContributor: SĂ©rgio Martins <sergio.martins@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef CLAZY_FIXIT_UTILS_H
0009 #define CLAZY_FIXIT_UTILS_H
0010 
0011 #include <clang/Basic/TokenKinds.h>
0012 #include <clang/Parse/Parser.h>
0013 
0014 #include <string>
0015 #include <vector>
0016 
0017 namespace clang
0018 {
0019 class ASTContext;
0020 class FixItHint;
0021 class SourceManager;
0022 class SourceRange;
0023 class SourceLocation;
0024 class StringLiteral;
0025 class CallExpr;
0026 class CXXMemberCallExpr;
0027 class Stmt;
0028 }
0029 
0030 namespace clazy
0031 {
0032 /**
0033  * Replaces whatever is in range, with replacement
0034  */
0035 clang::FixItHint createReplacement(clang::SourceRange range, const std::string &replacement);
0036 
0037 /**
0038  * Inserts insertion at start
0039  */
0040 clang::FixItHint createInsertion(clang::SourceLocation start, const std::string &insertion);
0041 
0042 /**
0043  * Transforms foo into method(foo), by inserting "method(" at the beginning, and ')' at the end
0044  */
0045 void insertParentMethodCall(const std::string &method, clang::SourceRange range, std::vector<clang::FixItHint> &fixits);
0046 
0047 /**
0048  * Transforms foo into method("literal"), by inserting "method(" at the beginning, and ')' at the end
0049  * Takes into account multi-token literals such as "foo""bar"
0050  */
0051 bool insertParentMethodCallAroundStringLiteral(const clang::ASTContext *context,
0052                                                const std::string &method,
0053                                                clang::StringLiteral *lt,
0054                                                std::vector<clang::FixItHint> &fixits);
0055 
0056 /**
0057  * Returns the range this literal spans. Takes into account multi token literals, such as "foo""bar"
0058  */
0059 clang::SourceRange rangeForLiteral(const clang::ASTContext *context, clang::StringLiteral *);
0060 
0061 /**
0062  * Goes through all children of stmt and finds the biggests source location.
0063  */
0064 clang::SourceLocation biggestSourceLocationInStmt(const clang::SourceManager &sm, clang::Stmt *stmt);
0065 
0066 clang::SourceLocation locForNextToken(const clang::ASTContext *context, clang::SourceLocation start, clang::tok::TokenKind kind);
0067 
0068 /**
0069  * Returns the end location of the token that starts at start.
0070  *
0071  * For example, having this expr:
0072  * getenv("FOO")
0073  *
0074  * ^              // expr->getLocStart()
0075  *             ^  // expr->getLocEnd()
0076  *      ^         // clazy::locForEndOfToken(expr->getLocStart())
0077  */
0078 clang::SourceLocation locForEndOfToken(const clang::ASTContext *context, clang::SourceLocation start, int offset = 0);
0079 
0080 /**
0081  * Transforms a call such as: foo("hello").bar() into baz("hello")
0082  */
0083 bool transformTwoCallsIntoOne(const clang::ASTContext *context,
0084                               clang::CallExpr *foo,
0085                               clang::CXXMemberCallExpr *bar,
0086                               const std::string &baz,
0087                               std::vector<clang::FixItHint> &fixits);
0088 
0089 /**
0090  * Transforms a call such as: foo("hello").bar() into baz()
0091  * This version basically replaces everything from start to end with baz.
0092  */
0093 bool transformTwoCallsIntoOneV2(const clang::ASTContext *context, clang::CXXMemberCallExpr *bar, const std::string &baz, std::vector<clang::FixItHint> &fixits);
0094 
0095 clang::FixItHint fixItReplaceWordWithWord(const clang::ASTContext *context, clang::Stmt *begin, const std::string &replacement, const std::string &replacee);
0096 
0097 std::vector<clang::FixItHint> fixItRemoveToken(const clang::ASTContext *context, clang::Stmt *stmt, bool removeParenthesis);
0098 
0099 }
0100 
0101 #endif