diff options
| author | Artem Metla <ametla@google.com> | 2026-02-02 15:31:52 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2026-02-17 17:06:23 +0000 |
| commit | 39751c2134a039db51aba39640ff82d072213d73 (patch) | |
| tree | 0341ebc367d12ec096864eb0de512cbde1121fea /tools/clang/codesearch/codesearch.cpp | |
| parent | e263b4d7a71c6049e9b187adb5d1309f366acd23 (diff) | |
tools/clang/codesearch: improve codesearch to handle global variables
Contributes to #6469.
To handle global variables:
* Add EntityKindGlobalVariable
* Modify TraverseVarDecl() function logic
* Add a check to ensure StartLine and EndLine are in the same file
* Fix missing #include <cstdint> in json.h
Diffstat (limited to 'tools/clang/codesearch/codesearch.cpp')
| -rw-r--r-- | tools/clang/codesearch/codesearch.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tools/clang/codesearch/codesearch.cpp b/tools/clang/codesearch/codesearch.cpp index a2d6883a2..00eac7c4c 100644 --- a/tools/clang/codesearch/codesearch.cpp +++ b/tools/clang/codesearch/codesearch.cpp @@ -161,6 +161,21 @@ Indexer::NamedDeclEmitter::NamedDeclEmitter(Indexer* Parent, const NamedDecl* De EndLine = std::max(EndLine, CommentEndLine); } } + + // Clang's SourceRange begin and end locations can resolve to different files + // when a declaration is produced by nested macro expansions (e.g. Linux kernel + // DEFINE_MUTEX, DEFINE_PER_CPU). This happens because getExpansionLoc() + // resolves each token independently to its outermost call site, and with + // multi-level macros the opening and closing tokens of the range may originate + // from different headers. EndLine < StartLine can occur when the end location + // is invalid or synthetic (e.g. compiler-generated declarations). In both + // cases, clamp to a single line so the database always records a valid, + // same-file range anchored at the macro invocation site. + if (EndLine < StartLine || + SM.getFileID(SM.getExpansionLoc(Range.getBegin())) != SM.getFileID(SM.getExpansionLoc(Range.getEnd()))) { + EndLine = StartLine; + } + Def = Definition{ .Kind = Kind, .Name = Decl->getNameAsString(), @@ -215,6 +230,15 @@ bool Indexer::VisitDeclRefExpr(const DeclRefExpr* DeclRef) { } bool Indexer::TraverseVarDecl(VarDecl* Decl) { + if (Decl->isFileVarDecl() && Decl->isThisDeclarationADefinition() == VarDecl::Definition) { + // Preserves whether this variable can be referenced from other translation + // units. A static global (internal linkage) is only referenceable within + // its own file, while a non-static global (external linkage) can be + // referenced from anywhere in the kernel. + const bool IsInternalLinkage = Decl->getStorageClass() == SC_Static; + NamedDeclEmitter Emitter(this, Decl, EntityKindGlobalVariable, Decl->getType().getAsString(), IsInternalLinkage); + } + ScopedState<SourceLocation> Scoped(&TypeRefingLocation, Decl->getBeginLoc()); return Base::TraverseVarDecl(Decl); } |
