10#include "clang/AST/AST.h"
11#include "clang/Basic/SourceManager.h"
17std::pair<Token, SourceLocation>
19 const LangOptions &LangOpts,
bool SkipComments) {
21 Token.setKind(tok::unknown);
27 auto StartOfFile = SM.getLocForStartOfFile(SM.getFileID(
Location));
30 if (!Lexer::getRawToken(
Location, Token, SM, LangOpts) &&
31 (!SkipComments || !Token.is(tok::comment))) {
40 const LangOptions &LangOpts,
bool SkipComments) {
47 const SourceManager &SM,
48 const LangOptions &LangOpts) {
49 if (Start.isInvalid() || Start.isMacroID())
52 SourceLocation BeforeStart = Start.getLocWithOffset(-1);
53 if (BeforeStart.isInvalid() || BeforeStart.isMacroID())
56 return Lexer::GetBeginningOfToken(BeforeStart, SM, LangOpts);
60 const SourceManager &SM,
61 const LangOptions &LangOpts,
63 if (Start.isInvalid() || Start.isMacroID())
68 if (L.isInvalid() || L.isMacroID())
72 if (Lexer::getRawToken(L, T, SM, LangOpts,
true))
76 return T.getLocation();
83 const LangOptions &LangOpts) {
89 const LangOptions &LangOpts) {
91 if (Start.isMacroID())
93 Start = Lexer::getLocForEndOfToken(Start, 0, SM, LangOpts);
95 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Start);
96 bool InvalidTemp =
false;
97 StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
101 Lexer L(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
102 File.data() + LocInfo.second, File.end());
103 L.SetCommentRetentionState(
true);
106 L.LexFromRawLexer(Tok);
112 const LangOptions &LangOpts) {
113 while (Start.isValid()) {
114 std::optional<Token> CurrentToken =
115 Lexer::findNextToken(Start, SM, LangOpts);
116 if (!CurrentToken || !CurrentToken->is(tok::comment))
119 Start = CurrentToken->getLocation();
126 const SourceManager &SM,
127 const LangOptions &LangOpts) {
128 assert(
Range.isValid() &&
"Invalid Range for relexing provided");
129 SourceLocation
Loc =
Range.getBegin();
135 std::optional<Token> Tok = Lexer::findNextToken(
Loc, SM, LangOpts);
140 if (Tok->is(tok::hash))
143 Loc = Tok->getLocation();
150 CharSourceRange
Range,
151 const ASTContext &Context,
152 const SourceManager &SM) {
153 assert((TK == tok::kw_const || TK == tok::kw_volatile ||
154 TK == tok::kw_restrict) &&
155 "TK is not a qualifier keyword");
156 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(
Range.getBegin());
157 StringRef File = SM.getBufferData(LocInfo.first);
158 Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), Context.getLangOpts(),
159 File.begin(), File.data() + LocInfo.second, File.end());
160 std::optional<Token> LastMatchBeforeTemplate;
161 std::optional<Token> LastMatchAfterTemplate;
162 bool SawTemplate =
false;
164 while (!RawLexer.LexFromRawLexer(Tok) &&
165 Range.getEnd() != Tok.getLocation() &&
166 !SM.isBeforeInTranslationUnit(
Range.getEnd(), Tok.getLocation())) {
167 if (Tok.is(tok::raw_identifier)) {
168 IdentifierInfo &
Info = Context.Idents.get(
169 StringRef(SM.getCharacterData(Tok.getLocation()), Tok.getLength()));
170 Tok.setIdentifierInfo(&
Info);
171 Tok.setKind(
Info.getTokenID());
173 if (Tok.is(tok::less))
175 else if (Tok.isOneOf(tok::greater, tok::greatergreater))
176 LastMatchAfterTemplate = std::nullopt;
177 else if (Tok.is(TK)) {
179 LastMatchAfterTemplate = Tok;
181 LastMatchBeforeTemplate = Tok;
184 return LastMatchAfterTemplate != std::nullopt ? LastMatchAfterTemplate
185 : LastMatchBeforeTemplate;
189 return isa<CompoundStmt, DeclStmt, NullStmt>(S);
193 return isa<Expr, DoStmt, ReturnStmt, BreakStmt, ContinueStmt, GotoStmt,
200 const SourceManager &SM,
201 const LangOptions &LangOpts) {
203 if (EndLoc.isMacroID()) {
209 const SourceLocation SpellingLoc = SM.getSpellingLoc(EndLoc);
210 std::optional<Token> NextTok =
215 if (NextTok && NextTok->is(tok::TokenKind::semi)) {
218 return NextTok->getLocation();
226 std::optional<Token> NextTok =
230 if (NextTok && NextTok->is(tok::TokenKind::semi))
231 return NextTok->getLocation();
237 const LangOptions &LangOpts) {
239 const Stmt *LastChild = &S;
242 for (
const Stmt *Child : LastChild->children())
249 return S.getEndLoc();
253 const SourceManager &SM) {
257 const LangOptions &LangOpts =
FuncDecl->getLangOpts();
259 if (
FuncDecl->getNumParams() == 0) {
263 SourceLocation CurrentLocation =
FuncDecl->getBeginLoc();
264 while (!Lexer::getRawToken(CurrentLocation, CurrentToken, SM, LangOpts,
266 if (CurrentToken.is(tok::r_paren))
267 return CurrentLocation.getLocWithOffset(1);
269 CurrentLocation = CurrentToken.getEndLoc();
278 const SourceLocation NoexceptLoc =
280 if (NoexceptLoc.isValid())
281 return Lexer::findLocationAfterToken(
282 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...
std::optional< Token > findNextTokenIncludingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
static constexpr const char FuncDecl[]