xref: /llvm-project/clang-tools-extra/clangd/ASTSignals.cpp (revision 042dd99484d6f393cc8a365def250e9d74c24d37)
1 //===--- ASTSignals.cpp ------------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ASTSignals.h"
10 #include "AST.h"
11 #include "FindTarget.h"
12 #include "Headers.h"
13 #include "support/Trace.h"
14 #include "clang/AST/DeclObjC.h"
15 
16 namespace clang {
17 namespace clangd {
derive(const ParsedAST & AST)18 ASTSignals ASTSignals::derive(const ParsedAST &AST) {
19   trace::Span Span("ASTSignals::derive");
20   ASTSignals Signals;
21   Signals.InsertionDirective = preferredIncludeDirective(
22       AST.tuPath(), AST.getLangOpts(),
23       AST.getIncludeStructure().MainFileIncludes, AST.getLocalTopLevelDecls());
24   const SourceManager &SM = AST.getSourceManager();
25   findExplicitReferences(
26       AST.getASTContext(),
27       [&](ReferenceLoc Ref) {
28         for (const NamedDecl *ND : Ref.Targets) {
29           if (!isInsideMainFile(Ref.NameLoc, SM))
30             continue;
31           SymbolID ID = getSymbolID(ND);
32           if (!ID)
33             continue;
34           unsigned &SymbolCount = Signals.ReferencedSymbols[ID];
35           SymbolCount++;
36           // Process namespace only when we see the symbol for the first time.
37           if (SymbolCount != 1)
38             continue;
39           if (const auto *NSD = dyn_cast<NamespaceDecl>(ND->getDeclContext())) {
40             if (NSD->isAnonymousNamespace())
41               continue;
42             std::string NS = printNamespaceScope(*NSD);
43             if (!NS.empty())
44               Signals.RelatedNamespaces[NS]++;
45           }
46         }
47       },
48       AST.getHeuristicResolver());
49   return Signals;
50 }
51 } // namespace clangd
52 } // namespace clang
53