summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/SourceCode.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/clangd/SourceCode.cpp')
-rw-r--r--clang-tools-extra/clangd/SourceCode.cpp47
1 files changed, 30 insertions, 17 deletions
diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index a0cec30f10a..e52588d6aa8 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -719,41 +719,53 @@ cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces,
return formatReplacements(Code, std::move(*CleanReplaces), Style);
}
-void lex(llvm::StringRef Code, const format::FormatStyle &Style,
- llvm::function_ref<void(const clang::Token &, Position)> Action) {
+void lex(llvm::StringRef Code, const LangOptions &LangOpts,
+ llvm::function_ref<void(const clang::Token &, const SourceManager &SM)>
+ Action) {
// FIXME: InMemoryFileAdapter crashes unless the buffer is null terminated!
std::string NullTerminatedCode = Code.str();
SourceManagerForFile FileSM("dummy.cpp", NullTerminatedCode);
auto &SM = FileSM.get();
auto FID = SM.getMainFileID();
- Lexer Lex(FID, SM.getBuffer(FID), SM, format::getFormattingLangOpts(Style));
+ // Create a raw lexer (with no associated preprocessor object).
+ Lexer Lex(FID, SM.getBuffer(FID), SM, LangOpts);
Token Tok;
while (!Lex.LexFromRawLexer(Tok))
- Action(Tok, sourceLocToPosition(SM, Tok.getLocation()));
+ Action(Tok, SM);
// LexFromRawLexer returns true after it lexes last token, so we still have
// one more token to report.
- Action(Tok, sourceLocToPosition(SM, Tok.getLocation()));
+ Action(Tok, SM);
}
llvm::StringMap<unsigned> collectIdentifiers(llvm::StringRef Content,
const format::FormatStyle &Style) {
llvm::StringMap<unsigned> Identifiers;
- lex(Content, Style, [&](const clang::Token &Tok, Position) {
- switch (Tok.getKind()) {
- case tok::identifier:
- ++Identifiers[Tok.getIdentifierInfo()->getName()];
- break;
- case tok::raw_identifier:
+ auto LangOpt = format::getFormattingLangOpts(Style);
+ lex(Content, LangOpt, [&](const clang::Token &Tok, const SourceManager &) {
+ if (Tok.getKind() == tok::raw_identifier)
++Identifiers[Tok.getRawIdentifier()];
- break;
- default:
- break;
- }
});
return Identifiers;
}
+std::vector<Range> collectIdentifierRanges(llvm::StringRef Identifier,
+ llvm::StringRef Content,
+ const LangOptions &LangOpts) {
+ std::vector<Range> Ranges;
+ lex(Content, LangOpts, [&](const clang::Token &Tok, const SourceManager &SM) {
+ if (Tok.getKind() != tok::raw_identifier)
+ return;
+ if (Tok.getRawIdentifier() != Identifier)
+ return;
+ auto Range = getTokenRange(SM, LangOpts, Tok.getLocation());
+ if (!Range)
+ return;
+ Ranges.push_back(*Range);
+ });
+ return Ranges;
+}
+
namespace {
struct NamespaceEvent {
enum {
@@ -786,8 +798,9 @@ void parseNamespaceEvents(llvm::StringRef Code,
std::string NSName;
NamespaceEvent Event;
- lex(Code, Style, [&](const clang::Token &Tok, Position P) {
- Event.Pos = std::move(P);
+ lex(Code, format::getFormattingLangOpts(Style),
+ [&](const clang::Token &Tok,const SourceManager &SM) {
+ Event.Pos = sourceLocToPosition(SM, Tok.getLocation());
switch (Tok.getKind()) {
case tok::raw_identifier:
// In raw mode, this could be a keyword or a name.
OpenPOWER on IntegriCloud