1 //===-- DataCollection.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/DataCollection.h"
12 #include "clang/Lex/Lexer.h"
15 namespace data_collection {
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());
24 // Add an empty space at the end as a padding to prevent
25 // that macro names concatenate to the names of other macros.
29 /// Returns a string that represents all macro expansions that expanded into the
30 /// given SourceLocation.
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();
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);
45 MacroStackStream.flush();
49 } // end namespace data_collection
50 } // end namespace clang