summaryrefslogtreecommitdiffstats
path: root/clang/lib/Tooling/Syntax/Tokens.cpp
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2019-12-11 15:40:23 +0100
committerSam McCall <sam.mccall@gmail.com>2019-12-12 12:59:50 +0100
commit3f8da5d0910772dc1f6198916a9141bf1d5be885 (patch)
tree56b6aac5d34696830de3a8a4fc7ba78e2b11130f /clang/lib/Tooling/Syntax/Tokens.cpp
parent471d9f3e698108da096bfcd85ac96e2eacda509b (diff)
downloadbcm5719-llvm-3f8da5d0910772dc1f6198916a9141bf1d5be885.tar.gz
bcm5719-llvm-3f8da5d0910772dc1f6198916a9141bf1d5be885.zip
[Tooling/Syntax] Helpers to find spelled tokens touching a location.
Summary: Useful when positions are used to target nodes, with before/after ambiguity. Reviewers: ilya-biryukov, kbobyrev Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D71356
Diffstat (limited to 'clang/lib/Tooling/Syntax/Tokens.cpp')
-rw-r--r--clang/lib/Tooling/Syntax/Tokens.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/clang/lib/Tooling/Syntax/Tokens.cpp b/clang/lib/Tooling/Syntax/Tokens.cpp
index 5941507e086..7c7a442dff0 100644
--- a/clang/lib/Tooling/Syntax/Tokens.cpp
+++ b/clang/lib/Tooling/Syntax/Tokens.cpp
@@ -248,6 +248,31 @@ TokenBuffer::expansionStartingAt(const syntax::Token *Spelled) const {
return E;
}
+llvm::ArrayRef<syntax::Token>
+syntax::spelledTokensTouching(SourceLocation Loc,
+ const syntax::TokenBuffer &Tokens) {
+ assert(Loc.isFileID());
+ llvm::ArrayRef<syntax::Token> All =
+ Tokens.spelledTokens(Tokens.sourceManager().getFileID(Loc));
+ // Comparing SourceLocations is well-defined within a FileID.
+ auto *Right = llvm::partition_point(
+ All, [&](const syntax::Token &Tok) { return Tok.location() < Loc; });
+ bool AcceptRight = Right != All.end() && Right->location() <= Loc;
+ bool AcceptLeft = Right != All.begin() && (Right - 1)->endLocation() >= Loc;
+ return llvm::makeArrayRef(Right - (AcceptLeft ? 1 : 0),
+ Right + (AcceptRight ? 1 : 0));
+}
+
+const syntax::Token *
+syntax::spelledIdentifierTouching(SourceLocation Loc,
+ const syntax::TokenBuffer &Tokens) {
+ for (const syntax::Token &Tok : spelledTokensTouching(Loc, Tokens)) {
+ if (Tok.kind() == tok::identifier)
+ return &Tok;
+ }
+ return nullptr;
+}
+
std::vector<const syntax::Token *>
TokenBuffer::macroExpansions(FileID FID) const {
auto FileIt = Files.find(FID);
OpenPOWER on IntegriCloud