10#include "clang/Basic/SourceManager.h"
18 if (Tok.is(tok::raw_identifier))
19 return Tok.getRawIdentifier();
20 if (
const IdentifierInfo *Info = Tok.getIdentifierInfo())
21 return Info->getName();
25std::vector<Token>
getRawTokens(CharSourceRange Range,
const SourceManager &SM,
26 const LangOptions &LangOpts) {
27 if (Range.isInvalid())
30 const CharSourceRange FileRange =
31 Lexer::makeFileCharRange(Range, SM, LangOpts);
32 if (FileRange.isInvalid())
36 const StringRef Text =
37 Lexer::getSourceText(FileRange, SM, LangOpts, &Invalid);
38 if (Invalid || Text.empty())
41 const auto [File, BeginOffset] = SM.getDecomposedLoc(FileRange.getBegin());
42 const StringRef Buffer = SM.getBufferData(File, &Invalid);
43 if (Invalid || BeginOffset + Text.size() > Buffer.size())
46 Lexer RawLexer(SM.getLocForStartOfFile(File), LangOpts, Buffer.begin(),
47 Buffer.begin() + BeginOffset, Buffer.end());
48 const size_t EndOffset = BeginOffset + Text.size();
49 std::vector<Token> Tokens;
51 while (!RawLexer.LexFromRawLexer(Tok) && Tok.isNot(tok::eof) &&
52 SM.getFileOffset(Tok.getLocation()) < EndOffset)
53 Tokens.push_back(Tok);
57std::pair<std::optional<Token>, SourceLocation>
59 const LangOptions &LangOpts,
bool SkipComments) {
60 const std::optional<Token> Tok =
61 Lexer::findPreviousToken(
Location, SM, LangOpts, !SkipComments);
64 return {*Tok, Lexer::GetBeginningOfToken(Tok->getLocation(), SM, LangOpts)};
66 return {std::nullopt, SourceLocation()};
70 const SourceManager &SM,
71 const LangOptions &LangOpts,
79 const SourceManager &SM,
80 const LangOptions &LangOpts) {
81 if (Start.isInvalid() || Start.isMacroID())
84 const SourceLocation BeforeStart = Start.getLocWithOffset(-1);
85 if (BeforeStart.isInvalid() || BeforeStart.isMacroID())
88 return Lexer::GetBeginningOfToken(BeforeStart, SM, LangOpts);
92 const SourceManager &SM,
93 const LangOptions &LangOpts,
95 if (Start.isInvalid() || Start.isMacroID())
100 if (L.isInvalid() || L.isMacroID())
104 if (Lexer::getRawToken(L, T, SM, LangOpts,
true))
108 return T.getLocation();
115 const LangOptions &LangOpts) {
120 const SourceManager &SM,
121 const LangOptions &LangOpts) {
122 assert(Range.isValid() &&
"Invalid Range for relexing provided");
123 SourceLocation Loc = Range.getBegin();
125 while (Loc <= Range.getEnd()) {
134 if (Tok->is(tok::hash))
137 Loc = Tok->getLocation();
144enum class CommentCollectionMode { AllComments, TrailingComments };
147static std::vector<CommentToken>
149 const LangOptions &LangOpts,
150 CommentCollectionMode Mode) {
151 std::vector<CommentToken> Comments;
152 if (Range.isInvalid())
155 const CharSourceRange FileRange =
156 Lexer::makeFileCharRange(Range, SM, LangOpts);
157 if (FileRange.isInvalid())
160 const std::pair<FileID, unsigned> BeginLoc =
161 SM.getDecomposedLoc(FileRange.getBegin());
162 const std::pair<FileID, unsigned> EndLoc =
163 SM.getDecomposedLoc(FileRange.getEnd());
165 if (BeginLoc.first != EndLoc.first)
168 bool Invalid =
false;
169 const StringRef Buffer = SM.getBufferData(BeginLoc.first, &Invalid);
173 const char *StrData = Buffer.data() + BeginLoc.second;
175 Lexer TheLexer(SM.getLocForStartOfFile(BeginLoc.first), LangOpts,
176 Buffer.begin(), StrData, Buffer.end());
179 TheLexer.SetCommentRetentionState(
true);
183 if (TheLexer.LexFromRawLexer(Tok))
185 if (Tok.is(tok::eof) || Tok.getLocation() == FileRange.getEnd() ||
186 SM.isBeforeInTranslationUnit(FileRange.getEnd(), Tok.getLocation()))
189 if (Tok.is(tok::comment)) {
190 const std::pair<FileID, unsigned> CommentLoc =
191 SM.getDecomposedLoc(Tok.getLocation());
192 assert(CommentLoc.first == BeginLoc.first);
195 StringRef(Buffer.begin() + CommentLoc.second, Tok.getLength()),
197 }
else if (Mode == CommentCollectionMode::TrailingComments) {
209 const SourceManager &SM,
210 const LangOptions &LangOpts) {
212 CommentCollectionMode::AllComments);
215std::vector<CommentToken>
217 const LangOptions &LangOpts) {
219 CommentCollectionMode::TrailingComments);
224 const LangOptions &LangOpts,
225 llvm::function_ref<
bool(
const Token &)> Pred) {
226 if (Range.isInvalid())
231 const CharSourceRange FileRange =
232 Lexer::makeFileCharRange(Range, SM, LangOpts);
233 if (FileRange.isInvalid())
236 const auto [BeginFID, BeginOffset] =
237 SM.getDecomposedLoc(FileRange.getBegin());
238 const auto [EndFID, EndOffset] = SM.getDecomposedLoc(FileRange.getEnd());
239 if (BeginFID != EndFID || BeginOffset > EndOffset)
242 bool Invalid =
false;
243 const StringRef Buffer = SM.getBufferData(BeginFID, &Invalid);
247 const char *LexStart = Buffer.data() + BeginOffset;
250 Lexer TheLexer(SM.getLocForStartOfFile(BeginFID), LangOpts, Buffer.begin(),
251 LexStart, Buffer.end());
252 TheLexer.SetCommentRetentionState(
true);
256 if (TheLexer.LexFromRawLexer(Tok))
259 if (Tok.is(tok::eof) || Tok.getLocation() == FileRange.getEnd() ||
260 SM.isBeforeInTranslationUnit(FileRange.getEnd(), Tok.getLocation()))
267 if (TheLexer.LexFromRawLexer(NextTok))
271 return CharSourceRange::getCharRange(Tok.getLocation(),
272 NextTok.getLocation());
277 CharSourceRange Range,
278 const ASTContext &Context,
279 const SourceManager &SM) {
280 assert((TK == tok::kw_const || TK == tok::kw_volatile ||
281 TK == tok::kw_restrict) &&
282 "TK is not a qualifier keyword");
283 const std::pair<FileID, unsigned> LocInfo =
284 SM.getDecomposedLoc(Range.getBegin());
285 const StringRef File = SM.getBufferData(LocInfo.first);
286 Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), Context.getLangOpts(),
287 File.begin(), File.data() + LocInfo.second, File.end());
288 std::optional<Token> LastMatchBeforeTemplate;
289 std::optional<Token> LastMatchAfterTemplate;
290 bool SawTemplate =
false;
292 while (!RawLexer.LexFromRawLexer(Tok) &&
293 Range.getEnd() != Tok.getLocation() &&
294 !SM.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation())) {
295 if (Tok.is(tok::raw_identifier)) {
296 IdentifierInfo &Info = Context.Idents.get(
297 StringRef(SM.getCharacterData(Tok.getLocation()), Tok.getLength()));
298 Tok.setIdentifierInfo(&Info);
299 Tok.setKind(Info.getTokenID());
301 if (Tok.is(tok::less)) {
303 }
else if (Tok.isOneOf(tok::greater, tok::greatergreater)) {
304 LastMatchAfterTemplate = std::nullopt;
305 }
else if (Tok.is(TK)) {
307 LastMatchAfterTemplate = Tok;
309 LastMatchBeforeTemplate = Tok;
312 return LastMatchAfterTemplate != std::nullopt ? LastMatchAfterTemplate
313 : LastMatchBeforeTemplate;
317 return isa<CompoundStmt, DeclStmt, NullStmt>(S);
321 return isa<Expr, DoStmt, ReturnStmt, BreakStmt, ContinueStmt, GotoStmt,
328 const SourceManager &SM,
329 const LangOptions &LangOpts) {
330 if (EndLoc.isMacroID()) {
336 const SourceLocation SpellingLoc = SM.getSpellingLoc(EndLoc);
337 std::optional<Token> NextTok =
342 if (NextTok && NextTok->is(tok::TokenKind::semi)) {
345 return NextTok->getLocation();
353 std::optional<Token> NextTok =
357 if (NextTok && NextTok->is(tok::TokenKind::semi))
358 return NextTok->getLocation();
364 const LangOptions &LangOpts) {
365 const Stmt *LastChild = &S;
368 for (
const Stmt *Child : LastChild->children())
375 return S.getEndLoc();
379 const SourceManager &SM) {
383 const LangOptions &LangOpts =
FuncDecl->getLangOpts();
385 if (
FuncDecl->getNumParams() == 0) {
389 SourceLocation CurrentLocation =
FuncDecl->getBeginLoc();
390 while (!Lexer::getRawToken(CurrentLocation, CurrentToken, SM, LangOpts,
392 if (CurrentToken.is(tok::r_paren))
393 return CurrentLocation.getLocWithOffset(1);
395 CurrentLocation = CurrentToken.getEndLoc();
404 const SourceLocation NoexceptLoc =
406 if (NoexceptLoc.isValid())
407 return Lexer::findLocationAfterToken(
408 NoexceptLoc, tok::r_paren, SM, LangOpts,
std::pair< std::optional< Token >, SourceLocation > getPreviousTokenAndStart(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
SourceLocation getLocationForNoexceptSpecifier(const FunctionDecl *FuncDecl, const SourceManager &SM)
For a given FunctionDecl returns the location where you would need to place the noexcept specifier.
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,...
std::vector< Token > getRawTokens(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns the raw tokens in Range.
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
StringRef getTokenName(const Token &Tok)
Returns the spelling of an identifier token.
std::optional< Token > getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or std::nullopt if not found.
std::vector< CommentToken > getTrailingCommentsInRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns comment tokens found in the given range. If a non-comment token is encountered,...
SourceLocation findNextAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
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)
CharSourceRange findTokenTextInRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts, llvm::function_ref< bool(const Token &)> Pred)
Returns source range of the first token in Range matching Pred. The returned char range starts at the...
static bool breakAndReturnEndPlus1Token(const Stmt &S)
static bool breakAndReturnEnd(const Stmt &S)
std::vector< CommentToken > getCommentsInRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns all comment tokens found in the given range.
static std::vector< CommentToken > collectCommentsInRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts, CommentCollectionMode Mode)
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[]