1 //===-- FindAllMacros.cpp - find all macros ---------------------*- 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 "FindAllMacros.h"
10 #include "HeaderMapCollector.h"
11 #include "PathConfig.h"
12 #include "SymbolInfo.h"
13 #include "clang/Basic/FileManager.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>
CreateMacroSymbol(const Token & MacroNameTok,const MacroInfo * info)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 
MacroDefined(const Token & MacroNameTok,const MacroDirective * MD)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 
MacroUsed(const Token & Name,const MacroDefinition & MD)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 
MacroExpands(const Token & MacroNameTok,const MacroDefinition & MD,SourceRange Range,const MacroArgs * Args)47 void FindAllMacros::MacroExpands(const Token &MacroNameTok,
48                                  const MacroDefinition &MD, SourceRange Range,
49                                  const MacroArgs *Args) {
50   MacroUsed(MacroNameTok, MD);
51 }
52 
Ifdef(SourceLocation Loc,const Token & MacroNameTok,const MacroDefinition & MD)53 void FindAllMacros::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
54                           const MacroDefinition &MD) {
55   MacroUsed(MacroNameTok, MD);
56 }
57 
Ifndef(SourceLocation Loc,const Token & MacroNameTok,const MacroDefinition & MD)58 void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
59                            const MacroDefinition &MD) {
60   MacroUsed(MacroNameTok, MD);
61 }
62 
EndOfMainFile()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
71