| 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 |
#include "FixItUtils.h" |
| 9 |
#include "StringUtils.h" |
| 10 |
|
| 11 |
#include <clang/AST/ASTContext.h> |
| 12 |
#include <clang/AST/Expr.h> |
| 13 |
#include <clang/AST/ExprCXX.h> |
| 14 |
#include <clang/AST/Stmt.h> |
| 15 |
#include <clang/Basic/Diagnostic.h> |
| 16 |
#include <clang/Basic/SourceLocation.h> |
| 17 |
#include <clang/Basic/SourceManager.h> |
| 18 |
#include <clang/Lex/Lexer.h> |
| 19 |
#include <clang/Lex/Token.h> |
| 20 |
|
| 21 |
using namespace clazy; |
| 22 |
using namespace clang; |
| 23 |
|
| 24 |
clang::FixItHint clazy::createReplacement(clang::SourceRange range, const std::string &replacement) |
| 25 |
{ |
| 26 |
if (range.getBegin().isInvalid()) { |
| 27 |
return {}; |
| 28 |
} |
| 29 |
return FixItHint::CreateReplacement(range, replacement); |
| 30 |
} |
| 31 |
|
| 32 |
clang::FixItHint clazy::createInsertion(clang::SourceLocation start, const std::string &insertion) |
| 33 |
{ |
| 34 |
if (start.isInvalid()) { |
| 35 |
return {}; |
| 36 |
} |
| 37 |
return FixItHint::CreateInsertion(start, insertion); |
| 38 |
} |
| 39 |
|
| 40 |
SourceRange clazy::rangeForLiteral(const ASTContext *context, StringLiteral *lt) |
| 41 |
{ |
| 42 |
if (!lt) { |
| 43 |
return {}; |
| 44 |
} |
| 45 |
|
| 46 |
const int numTokens = lt->getNumConcatenated(); |
| 47 |
const SourceLocation lastTokenLoc = lt->getStrTokenLoc(numTokens - 1); |
| 48 |
if (lastTokenLoc.isInvalid()) { |
| 49 |
return {}; |
| 50 |
} |
| 51 |
|
| 52 |
SourceRange range; |
| 53 |
range.setBegin(lt->getBeginLoc()); |
| 54 |
|
| 55 |
SourceLocation end = Lexer::getLocForEndOfToken(lastTokenLoc, |
| 56 |
0, |
| 57 |
context->getSourceManager(), |
| 58 |
context->getLangOpts()); // For some reason getLocStart(lt) is == to getLocEnd(lt) |
| 59 |
|
| 60 |
if (!end.isValid()) { |
| 61 |
return {}; |
| 62 |
} |
| 63 |
|
| 64 |
range.setEnd(end); |
| 65 |
return range; |
| 66 |
} |
| 67 |
|
| 68 |
void clazy::insertParentMethodCall(const std::string &method, SourceRange range, std::vector<FixItHint> &fixits) |
| 69 |
{ |
| 70 |
fixits.push_back(clazy::createInsertion(range.getEnd(), ")")); |
| 71 |
fixits.push_back(clazy::createInsertion(range.getBegin(), method + '(')); |
| 72 |
} |
| 73 |
|
| 74 |
bool clazy::insertParentMethodCallAroundStringLiteral(const ASTContext *context, const std::string &method, StringLiteral *lt, std::vector<FixItHint> &fixits) |
| 75 |
{ |
| 76 |
if (!lt) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
const SourceRange range = rangeForLiteral(context, lt); |
| 81 |
if (range.isInvalid()) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
insertParentMethodCall(method, range, /*by-ref*/ fixits); |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
SourceLocation clazy::locForNextToken(const ASTContext *context, SourceLocation start, tok::TokenKind kind) |
| 90 |
{ |
| 91 |
if (!start.isValid()) { |
| 92 |
return {}; |
| 93 |
} |
| 94 |
|
| 95 |
Token result; |
| 96 |
Lexer::getRawToken(start, result, context->getSourceManager(), context->getLangOpts()); |
| 97 |
|
| 98 |
if (result.getKind() == kind) { |
| 99 |
return start; |
| 100 |
} |
| 101 |
|
| 102 |
auto nextStart = Lexer::getLocForEndOfToken(start, 0, context->getSourceManager(), context->getLangOpts()); |
| 103 |
if (nextStart.getRawEncoding() == start.getRawEncoding()) { |
| 104 |
return {}; |
| 105 |
} |
| 106 |
|
| 107 |
return locForNextToken(context, nextStart, kind); |
| 108 |
} |
| 109 |
|
| 110 |
SourceLocation clazy::biggestSourceLocationInStmt(const SourceManager &sm, Stmt *stmt) |
| 111 |
{ |
| 112 |
if (!stmt) { |
| 113 |
return {}; |
| 114 |
} |
| 115 |
|
| 116 |
SourceLocation biggestLoc = stmt->getEndLoc(); |
| 117 |
|
| 118 |
for (auto *child : stmt->children()) { |
| 119 |
SourceLocation candidateLoc = biggestSourceLocationInStmt(sm, child); |
| 120 |
if (candidateLoc.isValid() && sm.isBeforeInSLocAddrSpace(biggestLoc, candidateLoc)) { |
| 121 |
biggestLoc = candidateLoc; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return biggestLoc; |
| 126 |
} |
| 127 |
|
| 128 |
SourceLocation clazy::locForEndOfToken(const ASTContext *context, SourceLocation start, int offset) |
| 129 |
{ |
| 130 |
return Lexer::getLocForEndOfToken(start, offset, context->getSourceManager(), context->getLangOpts()); |
| 131 |
} |
| 132 |
|
| 133 |
bool clazy::transformTwoCallsIntoOne(const ASTContext *context, |
| 134 |
CallExpr *call1, |
| 135 |
CXXMemberCallExpr *call2, |
| 136 |
const std::string &replacement, |
| 137 |
std::vector<FixItHint> &fixits) |
| 138 |
{ |
| 139 |
Expr *implicitArgument = call2->getImplicitObjectArgument(); |
| 140 |
if (!implicitArgument) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
const SourceLocation start1 = call1->getBeginLoc(); |
| 145 |
const SourceLocation end1 = clazy::locForEndOfToken(context, start1, -1); // -1 of offset, so we don't need to insert '(' |
| 146 |
if (end1.isInvalid()) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
const SourceLocation start2 = implicitArgument->getEndLoc(); |
| 151 |
const SourceLocation end2 = call2->getEndLoc(); |
| 152 |
if (start2.isInvalid() || end2.isInvalid()) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
// qgetenv("foo").isEmpty() |
| 157 |
// ^ start1 |
| 158 |
// ^ end1 |
| 159 |
// ^ start2 |
| 160 |
// ^ end2 |
| 161 |
fixits.push_back(clazy::createReplacement({start1, end1}, replacement)); |
| 162 |
fixits.push_back(clazy::createReplacement({start2, end2}, ")")); |
| 163 |
|
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
bool clazy::transformTwoCallsIntoOneV2(const ASTContext *context, CXXMemberCallExpr *call2, const std::string &replacement, std::vector<FixItHint> &fixits) |
| 168 |
{ |
| 169 |
Expr *implicitArgument = call2->getImplicitObjectArgument(); |
| 170 |
if (!implicitArgument) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
SourceLocation start = implicitArgument->getBeginLoc(); |
| 175 |
start = clazy::locForEndOfToken(context, start, 0); |
| 176 |
const SourceLocation end = call2->getEndLoc(); |
| 177 |
if (start.isInvalid() || end.isInvalid()) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
fixits.push_back(clazy::createReplacement({start, end}, replacement)); |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
FixItHint clazy::fixItReplaceWordWithWord(const ASTContext *context, clang::Stmt *begin, const std::string &replacement, const std::string &replacee) |
| 186 |
{ |
| 187 |
const auto &sm = context->getSourceManager(); |
| 188 |
SourceLocation rangeStart = begin->getBeginLoc(); |
| 189 |
SourceLocation rangeEnd = Lexer::getLocForEndOfToken(rangeStart, -1, sm, context->getLangOpts()); |
| 190 |
|
| 191 |
if (rangeEnd.isInvalid()) { |
| 192 |
// Fallback. Have seen a case in the wild where the above would fail, it's very rare |
| 193 |
rangeEnd = rangeStart.getLocWithOffset(replacee.size() - 2); |
| 194 |
if (rangeEnd.isInvalid()) { |
| 195 |
clazy::printLocation(sm, rangeStart); |
| 196 |
clazy::printLocation(sm, rangeEnd); |
| 197 |
clazy::printLocation(sm, Lexer::getLocForEndOfToken(rangeStart, 0, sm, context->getLangOpts())); |
| 198 |
return {}; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return FixItHint::CreateReplacement(SourceRange(rangeStart, rangeEnd), replacement); |
| 203 |
} |
| 204 |
|
| 205 |
std::vector<FixItHint> clazy::fixItRemoveToken(const ASTContext *context, Stmt *stmt, bool removeParenthesis) |
| 206 |
{ |
| 207 |
SourceLocation start = stmt->getBeginLoc(); |
| 208 |
SourceLocation end = Lexer::getLocForEndOfToken(start, removeParenthesis ? 0 : -1, context->getSourceManager(), context->getLangOpts()); |
| 209 |
|
| 210 |
std::vector<FixItHint> fixits; |
| 211 |
|
| 212 |
if (start.isValid() && end.isValid()) { |
| 213 |
fixits.push_back(FixItHint::CreateRemoval(SourceRange(start, end))); |
| 214 |
|
| 215 |
if (removeParenthesis) { |
| 216 |
// Remove the last parenthesis |
| 217 |
fixits.push_back(FixItHint::CreateRemoval(SourceRange(stmt->getEndLoc(), stmt->getEndLoc()))); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return fixits; |
| 222 |
} |