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 "support/Trace.h"
13 
14 namespace clang {
15 namespace clangd {
derive(const ParsedAST & AST)16 ASTSignals ASTSignals::derive(const ParsedAST &AST) {
17   trace::Span Span("ASTSignals::derive");
18   ASTSignals Signals;
19   const SourceManager &SM = AST.getSourceManager();
20   findExplicitReferences(
21       AST.getASTContext(),
22       [&](ReferenceLoc Ref) {
23         for (const NamedDecl *ND : Ref.Targets) {
24           if (!isInsideMainFile(Ref.NameLoc, SM))
25             continue;
26           SymbolID ID = getSymbolID(ND);
27           if (!ID)
28             continue;
29           unsigned &SymbolCount = Signals.ReferencedSymbols[ID];
30           SymbolCount++;
31           // Process namespace only when we see the symbol for the first time.
32           if (SymbolCount != 1)
33             continue;
34           if (const auto *NSD = dyn_cast<NamespaceDecl>(ND->getDeclContext())) {
35             if (NSD->isAnonymousNamespace())
36               continue;
37             std::string NS = printNamespaceScope(*NSD);
38             if (!NS.empty())
39               Signals.RelatedNamespaces[NS]++;
40           }
41         }
42       },
43       AST.getHeuristicResolver());
44   return Signals;
45 }
46 } // namespace clangd
47 } // namespace clang
48