| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Sérgio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef CLAZY_FIXIT_UTILS_H |
| 9 |
#define CLAZY_FIXIT_UTILS_H |
| 10 |
|
| 11 |
#include <clang/Basic/TokenKinds.h> |
| 12 |
#include <clang/Parse/Parser.h> |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
namespace clang |
| 18 |
{ |
| 19 |
class ASTContext; |
| 20 |
class FixItHint; |
| 21 |
class SourceManager; |
| 22 |
class SourceRange; |
| 23 |
class SourceLocation; |
| 24 |
class StringLiteral; |
| 25 |
class CallExpr; |
| 26 |
class CXXMemberCallExpr; |
| 27 |
class Stmt; |
| 28 |
} |
| 29 |
|
| 30 |
namespace clazy |
| 31 |
{ |
| 32 |
/** |
| 33 |
* Replaces whatever is in range, with replacement |
| 34 |
*/ |
| 35 |
clang::FixItHint createReplacement(clang::SourceRange range, const std::string &replacement); |
| 36 |
|
| 37 |
/** |
| 38 |
* Inserts insertion at start |
| 39 |
*/ |
| 40 |
clang::FixItHint createInsertion(clang::SourceLocation start, const std::string &insertion); |
| 41 |
|
| 42 |
/** |
| 43 |
* Transforms foo into method(foo), by inserting "method(" at the beginning, and ')' at the end |
| 44 |
*/ |
| 45 |
void insertParentMethodCall(const std::string &method, clang::SourceRange range, std::vector<clang::FixItHint> &fixits); |
| 46 |
|
| 47 |
/** |
| 48 |
* Transforms foo into method("literal"), by inserting "method(" at the beginning, and ')' at the end |
| 49 |
* Takes into account multi-token literals such as "foo""bar" |
| 50 |
*/ |
| 51 |
bool insertParentMethodCallAroundStringLiteral(const clang::ASTContext *context, |
| 52 |
const std::string &method, |
| 53 |
clang::StringLiteral *lt, |
| 54 |
std::vector<clang::FixItHint> &fixits); |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the range this literal spans. Takes into account multi token literals, such as "foo""bar" |
| 58 |
*/ |
| 59 |
clang::SourceRange rangeForLiteral(const clang::ASTContext *context, clang::StringLiteral *); |
| 60 |
|
| 61 |
/** |
| 62 |
* Goes through all children of stmt and finds the biggests source location. |
| 63 |
*/ |
| 64 |
clang::SourceLocation biggestSourceLocationInStmt(const clang::SourceManager &sm, clang::Stmt *stmt); |
| 65 |
|
| 66 |
clang::SourceLocation locForNextToken(const clang::ASTContext *context, clang::SourceLocation start, clang::tok::TokenKind kind); |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the end location of the token that starts at start. |
| 70 |
* |
| 71 |
* For example, having this expr: |
| 72 |
* getenv("FOO") |
| 73 |
* |
| 74 |
* ^ // expr->getLocStart() |
| 75 |
* ^ // expr->getLocEnd() |
| 76 |
* ^ // clazy::locForEndOfToken(expr->getLocStart()) |
| 77 |
*/ |
| 78 |
clang::SourceLocation locForEndOfToken(const clang::ASTContext *context, clang::SourceLocation start, int offset = 0); |
| 79 |
|
| 80 |
/** |
| 81 |
* Transforms a call such as: foo("hello").bar() into baz("hello") |
| 82 |
*/ |
| 83 |
bool transformTwoCallsIntoOne(const clang::ASTContext *context, |
| 84 |
clang::CallExpr *foo, |
| 85 |
clang::CXXMemberCallExpr *bar, |
| 86 |
const std::string &baz, |
| 87 |
std::vector<clang::FixItHint> &fixits); |
| 88 |
|
| 89 |
/** |
| 90 |
* Transforms a call such as: foo("hello").bar() into baz() |
| 91 |
* This version basically replaces everything from start to end with baz. |
| 92 |
*/ |
| 93 |
bool transformTwoCallsIntoOneV2(const clang::ASTContext *context, clang::CXXMemberCallExpr *bar, const std::string &baz, std::vector<clang::FixItHint> &fixits); |
| 94 |
|
| 95 |
clang::FixItHint fixItReplaceWordWithWord(const clang::ASTContext *context, clang::Stmt *begin, const std::string &replacement, const std::string &replacee); |
| 96 |
|
| 97 |
std::vector<clang::FixItHint> fixItRemoveToken(const clang::ASTContext *context, clang::Stmt *stmt, bool removeParenthesis); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
#endif |