1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
//===--- LexerUtils.cpp - clang-tidy---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "LexerUtils.h"
#include "clang/AST/AST.h"
#include "clang/Basic/SourceManager.h"
#include <optional>
namespace clang::tidy::utils::lexer {
Token getPreviousToken(SourceLocation Location, const SourceManager &SM,
const LangOptions &LangOpts, bool SkipComments) {
Token Token;
Token.setKind(tok::unknown);
Location = Location.getLocWithOffset(-1);
if (Location.isInvalid())
return Token;
auto StartOfFile = SM.getLocForStartOfFile(SM.getFileID(Location));
while (Location != StartOfFile) {
Location = Lexer::GetBeginningOfToken(Location, SM, LangOpts);
if (!Lexer::getRawToken(Location, Token, SM, LangOpts) &&
(!SkipComments || !Token.is(tok::comment))) {
break;
}
Location = Location.getLocWithOffset(-1);
}
return Token;
}
SourceLocation findPreviousTokenStart(SourceLocation Start,
const SourceManager &SM,
const LangOptions &LangOpts) {
if (Start.isInvalid() || Start.isMacroID())
return SourceLocation();
SourceLocation BeforeStart = Start.getLocWithOffset(-1);
if (BeforeStart.isInvalid() || BeforeStart.isMacroID())
return SourceLocation();
return Lexer::GetBeginningOfToken(BeforeStart, SM, LangOpts);
}
SourceLocation findPreviousTokenKind(SourceLocation Start,
const SourceManager &SM,
const LangOptions &LangOpts,
tok::TokenKind TK) {
if (Start.isInvalid() || Start.isMacroID())
return SourceLocation();
while (true) {
SourceLocation L = findPreviousTokenStart(Start, SM, LangOpts);
if (L.isInvalid() || L.isMacroID())
return SourceLocation();
Token T;
if (Lexer::getRawToken(L, T, SM, LangOpts, /*IgnoreWhiteSpace=*/true))
return SourceLocation();
if (T.is(TK))
return T.getLocation();
Start = L;
}
}
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM,
const LangOptions &LangOpts) {
return findNextAnyTokenKind(Start, SM, LangOpts, tok::comma, tok::semi);
}
std::optional<Token>
findNextTokenIncludingComments(SourceLocation Start, const SourceManager &SM,
const LangOptions &LangOpts) {
// `Lexer::findNextToken` will ignore comment
if (Start.isMacroID())
return std::nullopt;
Start = Lexer::getLocForEndOfToken(Start, 0, SM, LangOpts);
// Break down the source location.
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Start);
bool InvalidTemp = false;
StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
if (InvalidTemp)
return std::nullopt;
// Lex from the start of the given location.
Lexer L(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
File.data() + LocInfo.second, File.end());
L.SetCommentRetentionState(true);
// Find the token.
Token Tok;
L.LexFromRawLexer(Tok);
return Tok;
}
std::optional<Token>
findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM,
const LangOptions &LangOpts) {
while (Start.isValid()) {
std::optional<Token> CurrentToken =
Lexer::findNextToken(Start, SM, LangOpts);
if (!CurrentToken || !CurrentToken->is(tok::comment))
return CurrentToken;
Start = CurrentToken->getLocation();
}
return std::nullopt;
}
bool rangeContainsExpansionsOrDirectives(SourceRange Range,
const SourceManager &SM,
const LangOptions &LangOpts) {
assert(Range.isValid() && "Invalid Range for relexing provided");
SourceLocation Loc = Range.getBegin();
while (Loc <= Range.getEnd()) {
if (Loc.isMacroID())
return true;
std::optional<Token> Tok = Lexer::findNextToken(Loc, SM, LangOpts);
if (!Tok)
return true;
if (Tok->is(tok::hash))
return true;
Loc = Tok->getLocation();
}
return false;
}
std::optional<Token> getQualifyingToken(tok::TokenKind TK,
CharSourceRange Range,
const ASTContext &Context,
const SourceManager &SM) {
assert((TK == tok::kw_const || TK == tok::kw_volatile ||
TK == tok::kw_restrict) &&
"TK is not a qualifier keyword");
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getBegin());
StringRef File = SM.getBufferData(LocInfo.first);
Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), Context.getLangOpts(),
File.begin(), File.data() + LocInfo.second, File.end());
std::optional<Token> LastMatchBeforeTemplate;
std::optional<Token> LastMatchAfterTemplate;
bool SawTemplate = false;
Token Tok;
while (!RawLexer.LexFromRawLexer(Tok) &&
Range.getEnd() != Tok.getLocation() &&
!SM.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation())) {
if (Tok.is(tok::raw_identifier)) {
IdentifierInfo &Info = Context.Idents.get(
StringRef(SM.getCharacterData(Tok.getLocation()), Tok.getLength()));
Tok.setIdentifierInfo(&Info);
Tok.setKind(Info.getTokenID());
}
if (Tok.is(tok::less))
SawTemplate = true;
else if (Tok.isOneOf(tok::greater, tok::greatergreater))
LastMatchAfterTemplate = std::nullopt;
else if (Tok.is(TK)) {
if (SawTemplate)
LastMatchAfterTemplate = Tok;
else
LastMatchBeforeTemplate = Tok;
}
}
return LastMatchAfterTemplate != std::nullopt ? LastMatchAfterTemplate
: LastMatchBeforeTemplate;
}
static bool breakAndReturnEnd(const Stmt &S) {
return isa<CompoundStmt, DeclStmt, NullStmt>(S);
}
static bool breakAndReturnEndPlus1Token(const Stmt &S) {
return isa<Expr, DoStmt, ReturnStmt, BreakStmt, ContinueStmt, GotoStmt,
SEHLeaveStmt>(S);
}
// Given a Stmt which does not include it's semicolon this method returns the
// SourceLocation of the semicolon.
static SourceLocation getSemicolonAfterStmtEndLoc(const SourceLocation &EndLoc,
const SourceManager &SM,
const LangOptions &LangOpts) {
if (EndLoc.isMacroID()) {
// Assuming EndLoc points to a function call foo within macro F.
// This method is supposed to return location of the semicolon within
// those macro arguments:
// F ( foo() ; )
// ^ EndLoc ^ SpellingLoc ^ next token of SpellingLoc
const SourceLocation SpellingLoc = SM.getSpellingLoc(EndLoc);
std::optional<Token> NextTok =
findNextTokenSkippingComments(SpellingLoc, SM, LangOpts);
// Was the next token found successfully?
// All macro issues are simply resolved by ensuring it's a semicolon.
if (NextTok && NextTok->is(tok::TokenKind::semi)) {
// Ideally this would return `F` with spelling location `;` (NextTok)
// following the example above. For now simply return NextTok location.
return NextTok->getLocation();
}
// Fallthrough to 'normal handling'.
// F ( foo() ) ;
// ^ EndLoc ^ SpellingLoc ) ^ next token of EndLoc
}
std::optional<Token> NextTok =
findNextTokenSkippingComments(EndLoc, SM, LangOpts);
// Testing for semicolon again avoids some issues with macros.
if (NextTok && NextTok->is(tok::TokenKind::semi))
return NextTok->getLocation();
return SourceLocation();
}
SourceLocation getUnifiedEndLoc(const Stmt &S, const SourceManager &SM,
const LangOptions &LangOpts) {
const Stmt *LastChild = &S;
while (!LastChild->children().empty() && !breakAndReturnEnd(*LastChild) &&
!breakAndReturnEndPlus1Token(*LastChild)) {
for (const Stmt *Child : LastChild->children())
LastChild = Child;
}
if (!breakAndReturnEnd(*LastChild) && breakAndReturnEndPlus1Token(*LastChild))
return getSemicolonAfterStmtEndLoc(S.getEndLoc(), SM, LangOpts);
return S.getEndLoc();
}
SourceLocation getLocationForNoexceptSpecifier(const FunctionDecl *FuncDecl,
const SourceManager &SM) {
if (!FuncDecl)
return {};
const LangOptions &LangOpts = FuncDecl->getLangOpts();
if (FuncDecl->getNumParams() == 0) {
// Start at the beginning of the function declaration, and find the closing
// parenthesis after which we would place the noexcept specifier.
Token CurrentToken;
SourceLocation CurrentLocation = FuncDecl->getBeginLoc();
while (!Lexer::getRawToken(CurrentLocation, CurrentToken, SM, LangOpts,
true)) {
if (CurrentToken.is(tok::r_paren))
return CurrentLocation.getLocWithOffset(1);
CurrentLocation = CurrentToken.getEndLoc();
}
// Failed to find the closing parenthesis, so just return an invalid
// SourceLocation.
return {};
}
// FunctionDecl with parameters
const SourceLocation NoexceptLoc =
FuncDecl->getParamDecl(FuncDecl->getNumParams() - 1)->getEndLoc();
if (NoexceptLoc.isValid())
return Lexer::findLocationAfterToken(
NoexceptLoc, tok::r_paren, SM, LangOpts,
/*SkipTrailingWhitespaceAndNewLine=*/true);
return {};
}
} // namespace clang::tidy::utils::lexer
|