blob: c2ecabe8e6f8adac964a313261fe8cb2500fd06f [file] [log] [blame]
Johannes Altmanninger1a267692017-08-23 16:28:26 +00001//===-- 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
14namespace clang {
15namespace data_collection {
16
17/// Prints the macro name that contains the given SourceLocation into the given
18/// raw_string_ostream.
19static 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.
34std::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