clang  6.0.0svn
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 
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
SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const
Gets the location of the immediate macro caller, one level up the stack toward the initial macro type...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
std::string getMacroStack(SourceLocation Loc, ASTContext &Context)
Returns a string that represents all macro expansions that expanded into the given SourceLocation...
This file declares helper methods for collecting data from AST nodes.
const SourceManager & SM
Definition: Format.cpp:1337
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.
Encodes a location in the source.
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition: Lexer.cpp:970
Dataflow Directional Tag Classes.
bool isMacroID() const
SourceManager & getSourceManager()
Definition: ASTContext.h:643
const LangOptions & getLangOpts() const
Definition: ASTContext.h:688
This class handles loading and caching of source files into memory.