1*0b57cec5SDimitry Andric //===-- DataCollection.cpp --------------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "clang/AST/DataCollection.h"
10*0b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h"
11*0b57cec5SDimitry Andric #include "clang/Lex/Lexer.h"
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric namespace clang {
14*0b57cec5SDimitry Andric namespace data_collection {
15*0b57cec5SDimitry Andric
16*0b57cec5SDimitry Andric /// Prints the macro name that contains the given SourceLocation into the given
17*0b57cec5SDimitry Andric /// raw_string_ostream.
printMacroName(llvm::raw_string_ostream & MacroStack,ASTContext & Context,SourceLocation Loc)18*0b57cec5SDimitry Andric static void printMacroName(llvm::raw_string_ostream &MacroStack,
19*0b57cec5SDimitry Andric ASTContext &Context, SourceLocation Loc) {
20*0b57cec5SDimitry Andric MacroStack << Lexer::getImmediateMacroName(Loc, Context.getSourceManager(),
21*0b57cec5SDimitry Andric Context.getLangOpts());
22*0b57cec5SDimitry Andric
23*0b57cec5SDimitry Andric // Add an empty space at the end as a padding to prevent
24*0b57cec5SDimitry Andric // that macro names concatenate to the names of other macros.
25*0b57cec5SDimitry Andric MacroStack << " ";
26*0b57cec5SDimitry Andric }
27*0b57cec5SDimitry Andric
28*0b57cec5SDimitry Andric /// Returns a string that represents all macro expansions that expanded into the
29*0b57cec5SDimitry Andric /// given SourceLocation.
30*0b57cec5SDimitry Andric ///
31*0b57cec5SDimitry Andric /// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations
32*0b57cec5SDimitry Andric /// A and B are expanded from the same macros in the same order.
getMacroStack(SourceLocation Loc,ASTContext & Context)33*0b57cec5SDimitry Andric std::string getMacroStack(SourceLocation Loc, ASTContext &Context) {
34*0b57cec5SDimitry Andric std::string MacroStack;
35*0b57cec5SDimitry Andric llvm::raw_string_ostream MacroStackStream(MacroStack);
36*0b57cec5SDimitry Andric SourceManager &SM = Context.getSourceManager();
37*0b57cec5SDimitry Andric
38*0b57cec5SDimitry Andric // Iterate over all macros that expanded into the given SourceLocation.
39*0b57cec5SDimitry Andric while (Loc.isMacroID()) {
40*0b57cec5SDimitry Andric // Add the macro name to the stream.
41*0b57cec5SDimitry Andric printMacroName(MacroStackStream, Context, Loc);
42*0b57cec5SDimitry Andric Loc = SM.getImmediateMacroCallerLoc(Loc);
43*0b57cec5SDimitry Andric }
44*0b57cec5SDimitry Andric MacroStackStream.flush();
45*0b57cec5SDimitry Andric return MacroStack;
46*0b57cec5SDimitry Andric }
47*0b57cec5SDimitry Andric
48*0b57cec5SDimitry Andric } // end namespace data_collection
49*0b57cec5SDimitry Andric } // end namespace clang
50