10#include "clang/AST/AST.h"
11#include "clang/Basic/SourceManager.h"
17std::pair<Token, SourceLocation>
19 const LangOptions &LangOpts,
bool SkipComments) {
20 const std::optional<Token> Tok =
21 Lexer::findPreviousToken(
Location, SM, LangOpts, !SkipComments);
23 if (Tok.has_value()) {
24 return {*Tok, Lexer::GetBeginningOfToken(Tok->getLocation(), SM, LangOpts)};
28 Token.setKind(tok::unknown);
29 return {Token, SourceLocation()};
33 const LangOptions &LangOpts,
bool SkipComments) {
40 const SourceManager &SM,
41 const LangOptions &LangOpts) {
42 if (Start.isInvalid() || Start.isMacroID())
45 SourceLocation BeforeStart = Start.getLocWithOffset(-1);
46 if (BeforeStart.isInvalid() || BeforeStart.isMacroID())
49 return Lexer::GetBeginningOfToken(BeforeStart, SM, LangOpts);
53 const SourceManager &SM,
54 const LangOptions &LangOpts,
56 if (Start.isInvalid() || Start.isMacroID())
61 if (L.isInvalid() || L.isMacroID())
65 if (Lexer::getRawToken(L, T, SM, LangOpts,
true))
69 return T.getLocation();
76 const LangOptions &LangOpts) {
82 const LangOptions &LangOpts) {
83 while (Start.isValid()) {
84 std::optional<Token> CurrentToken =
85 Lexer::findNextToken(Start, SM, LangOpts);
86 if (!CurrentToken || !CurrentToken->is(tok::comment))
89 Start = CurrentToken->getLocation();
96 const SourceManager &SM,
97 const LangOptions &LangOpts) {
98 assert(
Range.isValid() &&
"Invalid Range for relexing provided");
99 SourceLocation
Loc =
Range.getBegin();
105 std::optional<Token> Tok = Lexer::findNextToken(
Loc, SM, LangOpts);
110 if (Tok->is(tok::hash))
113 Loc = Tok->getLocation();
120 CharSourceRange
Range,
121 const ASTContext &Context,
122 const SourceManager &SM) {
123 assert((TK == tok::kw_const || TK == tok::kw_volatile ||
124 TK == tok::kw_restrict) &&
125 "TK is not a qualifier keyword");
126 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(
Range.getBegin());
127 StringRef File = SM.getBufferData(LocInfo.first);
128 Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), Context.getLangOpts(),
129 File.begin(), File.data() + LocInfo.second, File.end());
130 std::optional<Token> LastMatchBeforeTemplate;
131 std::optional<Token> LastMatchAfterTemplate;
132 bool SawTemplate =
false;
134 while (!RawLexer.LexFromRawLexer(Tok) &&
135 Range.getEnd() != Tok.getLocation() &&
136 !SM.isBeforeInTranslationUnit(
Range.getEnd(), Tok.getLocation())) {
137 if (Tok.is(tok::raw_identifier)) {
138 IdentifierInfo &
Info = Context.Idents.get(
139 StringRef(SM.getCharacterData(Tok.getLocation()), Tok.getLength()));
140 Tok.setIdentifierInfo(&
Info);
141 Tok.setKind(
Info.getTokenID());
143 if (Tok.is(tok::less))
145 else if (Tok.isOneOf(tok::greater, tok::greatergreater))
146 LastMatchAfterTemplate = std::nullopt;
147 else if (Tok.is(TK)) {
149 LastMatchAfterTemplate = Tok;
151 LastMatchBeforeTemplate = Tok;
154 return LastMatchAfterTemplate != std::nullopt ? LastMatchAfterTemplate
155 : LastMatchBeforeTemplate;
159 return isa<CompoundStmt, DeclStmt, NullStmt>(S);
163 return isa<Expr, DoStmt, ReturnStmt, BreakStmt, ContinueStmt, GotoStmt,
170 const SourceManager &SM,
171 const LangOptions &LangOpts) {
173 if (EndLoc.isMacroID()) {
179 const SourceLocation SpellingLoc = SM.getSpellingLoc(EndLoc);
180 std::optional<Token> NextTok =
185 if (NextTok && NextTok->is(tok::TokenKind::semi)) {
188 return NextTok->getLocation();
196 std::optional<Token> NextTok =
200 if (NextTok && NextTok->is(tok::TokenKind::semi))
201 return NextTok->getLocation();
207 const LangOptions &LangOpts) {
209 const Stmt *LastChild = &S;
212 for (
const Stmt *Child : LastChild->children())
219 return S.getEndLoc();
223 const SourceManager &SM) {
227 const LangOptions &LangOpts =
FuncDecl->getLangOpts();
229 if (
FuncDecl->getNumParams() == 0) {
233 SourceLocation CurrentLocation =
FuncDecl->getBeginLoc();
234 while (!Lexer::getRawToken(CurrentLocation, CurrentToken, SM, LangOpts,
236 if (CurrentToken.is(tok::r_paren))
237 return CurrentLocation.getLocWithOffset(1);
239 CurrentLocation = CurrentToken.getEndLoc();
248 const SourceLocation NoexceptLoc =
250 if (NoexceptLoc.isValid())
251 return Lexer::findLocationAfterToken(
252 NoexceptLoc, tok::r_paren, SM, LangOpts,
CharSourceRange Range
SourceRange for the file name.
SourceLocation getLocationForNoexceptSpecifier(const FunctionDecl *FuncDecl, const SourceManager &SM)
For a given FunctionDecl returns the location where you would need to place the noexcept specifier.
std::pair< Token, SourceLocation > getPreviousTokenAndStart(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
SourceLocation getUnifiedEndLoc(const Stmt &S, const SourceManager &SM, const LangOptions &LangOpts)
Stmt->getEndLoc does not always behave the same way depending on Token type.
bool rangeContainsExpansionsOrDirectives(SourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Re-lex the provide Range and return false if either a macro spans multiple tokens,...
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
SourceLocation findNextAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
Token getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or tok::unknown if not found.
std::optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
SourceLocation findPreviousTokenStart(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
static SourceLocation getSemicolonAfterStmtEndLoc(const SourceLocation &EndLoc, const SourceManager &SM, const LangOptions &LangOpts)
static bool breakAndReturnEndPlus1Token(const Stmt &S)
static bool breakAndReturnEnd(const Stmt &S)
SourceLocation findPreviousTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, tok::TokenKind TK)
std::optional< Token > getQualifyingToken(tok::TokenKind TK, CharSourceRange Range, const ASTContext &Context, const SourceManager &SM)
Assuming that Range spans a CVR-qualified type, returns the token in Range that is responsible for th...
static constexpr const char FuncDecl[]