clang-tools 8.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
63 void FindAllMacros::EndOfMainFile() {
64 Reporter->reportSymbols(SM->getFileEntryForID(SM->getMainFileID())->getName(),
65 FileSymbols);
66 FileSymbols.clear();
67 }
68
69 } // namespace find_all_symbols
70 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::find_all_symbols::FindAllMacros::Ifdef
void Ifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
Definition: FindAllMacros.cpp:53
clang::find_all_symbols::SymbolReporter::reportSymbols
virtual void reportSymbols(llvm::StringRef FileName, const SymbolInfo::SignalMap &Symbols)=0
HeaderMapCollector.h
clang::find_all_symbols::FindAllMacros::MacroExpands
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override
Definition: FindAllMacros.cpp:47
clang::find_all_symbols::getIncludePath
std::string getIncludePath(const SourceManager &SM, SourceLocation Loc, const HeaderMapCollector *Collector)
This calculates the include path for Loc.
Definition: PathConfig.cpp:17
clang::find_all_symbols::FindAllMacros::EndOfMainFile
void EndOfMainFile() override
Definition: FindAllMacros.cpp:63
SymbolInfo.h
clang::find_all_symbols::FindAllMacros::Ifndef
void Ifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
Definition: FindAllMacros.cpp:58
SymbolInfo
clang::find_all_symbols::SymbolInfo SymbolInfo
Definition: FindAllSymbolsMain.cpp:39
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::find_all_symbols::FindAllMacros::MacroDefined
void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override
Definition: FindAllMacros.cpp:34
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::doc::MD
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::find_all_symbols::SymbolInfo::SymbolKind::Macro
FindAllMacros.h
PathConfig.h