clang-tools  7.0.0
FindAllMacros.cpp
Go to the documentation of this file.
1 //===-- FindAllMacros.cpp - find all macros ---------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FindAllMacros.h"
11 #include "HeaderMapCollector.h"
12 #include "PathConfig.h"
13 #include "SymbolInfo.h"
14 #include "clang/Basic/IdentifierTable.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Lex/MacroInfo.h"
17 #include "clang/Lex/Token.h"
18 #include "llvm/Support/Path.h"
19 
20 namespace clang {
21 namespace find_all_symbols {
22 
23 llvm::Optional<SymbolInfo>
24 FindAllMacros::CreateMacroSymbol(const Token &MacroNameTok,
25  const MacroInfo *info) {
26  std::string FilePath =
27  getIncludePath(*SM, info->getDefinitionLoc(), Collector);
28  if (FilePath.empty())
29  return llvm::None;
30  return SymbolInfo(MacroNameTok.getIdentifierInfo()->getName(),
31  SymbolInfo::SymbolKind::Macro, FilePath, {});
32 }
33 
34 void FindAllMacros::MacroDefined(const Token &MacroNameTok,
35  const MacroDirective *MD) {
36  if (auto Symbol = CreateMacroSymbol(MacroNameTok, MD->getMacroInfo()))
37  ++FileSymbols[*Symbol].Seen;
38 }
39 
40 void FindAllMacros::MacroUsed(const Token &Name, const MacroDefinition &MD) {
41  if (!MD || !SM->isInMainFile(SM->getExpansionLoc(Name.getLocation())))
42  return;
43  if (auto Symbol = CreateMacroSymbol(Name, MD.getMacroInfo()))
44  ++FileSymbols[*Symbol].Used;
45 }
46 
47 void FindAllMacros::MacroExpands(const Token &MacroNameTok,
48  const MacroDefinition &MD, SourceRange Range,
49  const MacroArgs *Args) {
50  MacroUsed(MacroNameTok, MD);
51 }
52 
53 void FindAllMacros::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
54  const MacroDefinition &MD) {
55  MacroUsed(MacroNameTok, MD);
56 }
57 
58 void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
59  const MacroDefinition &MD) {
60  MacroUsed(MacroNameTok, MD);
61 }
62 
64  Reporter->reportSymbols(SM->getFileEntryForID(SM->getMainFileID())->getName(),
65  FileSymbols);
66  FileSymbols.clear();
67 }
68 
69 } // namespace find_all_symbols
70 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
llvm::StringRef Name
void Ifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
virtual void reportSymbols(llvm::StringRef FileName, const SymbolInfo::SignalMap &Symbols)=0
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override
std::string getIncludePath(const SourceManager &SM, SourceLocation Loc, const HeaderMapCollector *Collector)
This calculates the include path for Loc.
Definition: PathConfig.cpp:17
void Ifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
clang::find_all_symbols::SymbolInfo SymbolInfo
void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.