clang 8.0.0

DataCollection.cpp
Go to the documentation of this file.
1 //===-- DataCollection.cpp --------------------------------------*- 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 "clang/AST/DataCollection.h"
11
12 #include "clang/Lex/Lexer.h"
13
14 namespace clang {
15 namespace data_collection {
16
17 /// Prints the macro name that contains the given SourceLocation into the given
18 /// raw_string_ostream.
19 static void printMacroName(llvm::raw_string_ostream &MacroStack,
20 ASTContext &Context, SourceLocation Loc) {
21 MacroStack << Lexer::getImmediateMacroName(Loc, Context.getSourceManager(),
22 Context.getLangOpts());
23
24 // Add an empty space at the end as a padding to prevent
25 // that macro names concatenate to the names of other macros.
26 MacroStack << " ";
27 }
28
29 /// Returns a string that represents all macro expansions that expanded into the
30 /// given SourceLocation.
31 ///
32 /// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations
33 /// A and B are expanded from the same macros in the same order.
34 std::string getMacroStack(SourceLocation Loc, ASTContext &Context) {
35 std::string MacroStack;
36 llvm::raw_string_ostream MacroStackStream(MacroStack);
37 SourceManager &SM = Context.getSourceManager();
38
39 // Iterate over all macros that expanded into the given SourceLocation.
40 while (Loc.isMacroID()) {
41 // Add the macro name to the stream.
42 printMacroName(MacroStackStream, Context, Loc);
43 Loc = SM.getImmediateMacroCallerLoc(Loc);
44 }
45 MacroStackStream.flush();
46 return MacroStack;
47 }
48
49 } // end namespace data_collection
50 } // end namespace clang
clang::SourceManager::getImmediateMacroCallerLoc
SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const
Gets the location of the immediate macro caller, one level up the stack toward the initial macro type...
Definition: SourceManager.h:1687
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::data_collection::getMacroStack
std::string getMacroStack(SourceLocation Loc, ASTContext &Context)
Returns a string that represents all macro expansions that expanded into the given SourceLocation...
Definition: DataCollection.cpp:34
DataCollection.h
This file declares helper methods for collecting data from AST nodes.
Lexer.h
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::data_collection::printMacroName
static void printMacroName(llvm::raw_string_ostream &MacroStack, ASTContext &Context, SourceLocation Loc)
Prints the macro name that contains the given SourceLocation into the given raw_string_ostream.
Definition: DataCollection.cpp:19
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::Lexer::getImmediateMacroName
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition: Lexer.cpp:968
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::SourceLocation::isMacroID
bool isMacroID() const
Definition: SourceLocation.h:101
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:662
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:707
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582