11a267692SJohannes Altmanninger //===-- DataCollection.cpp --------------------------------------*- C++ -*-===// 21a267692SJohannes Altmanninger // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61a267692SJohannes Altmanninger // 71a267692SJohannes Altmanninger //===----------------------------------------------------------------------===// 81a267692SJohannes Altmanninger 91a267692SJohannes Altmanninger #include "clang/AST/DataCollection.h" 10*86565c13SReid Kleckner #include "clang/Basic/SourceManager.h" 111a267692SJohannes Altmanninger #include "clang/Lex/Lexer.h" 121a267692SJohannes Altmanninger 131a267692SJohannes Altmanninger namespace clang { 141a267692SJohannes Altmanninger namespace data_collection { 151a267692SJohannes Altmanninger 161a267692SJohannes Altmanninger /// Prints the macro name that contains the given SourceLocation into the given 171a267692SJohannes Altmanninger /// raw_string_ostream. 181a267692SJohannes Altmanninger static void printMacroName(llvm::raw_string_ostream &MacroStack, 191a267692SJohannes Altmanninger ASTContext &Context, SourceLocation Loc) { 201a267692SJohannes Altmanninger MacroStack << Lexer::getImmediateMacroName(Loc, Context.getSourceManager(), 211a267692SJohannes Altmanninger Context.getLangOpts()); 221a267692SJohannes Altmanninger 231a267692SJohannes Altmanninger // Add an empty space at the end as a padding to prevent 241a267692SJohannes Altmanninger // that macro names concatenate to the names of other macros. 251a267692SJohannes Altmanninger MacroStack << " "; 261a267692SJohannes Altmanninger } 271a267692SJohannes Altmanninger 281a267692SJohannes Altmanninger /// Returns a string that represents all macro expansions that expanded into the 291a267692SJohannes Altmanninger /// given SourceLocation. 301a267692SJohannes Altmanninger /// 311a267692SJohannes Altmanninger /// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations 321a267692SJohannes Altmanninger /// A and B are expanded from the same macros in the same order. 331a267692SJohannes Altmanninger std::string getMacroStack(SourceLocation Loc, ASTContext &Context) { 341a267692SJohannes Altmanninger std::string MacroStack; 351a267692SJohannes Altmanninger llvm::raw_string_ostream MacroStackStream(MacroStack); 361a267692SJohannes Altmanninger SourceManager &SM = Context.getSourceManager(); 371a267692SJohannes Altmanninger 381a267692SJohannes Altmanninger // Iterate over all macros that expanded into the given SourceLocation. 391a267692SJohannes Altmanninger while (Loc.isMacroID()) { 401a267692SJohannes Altmanninger // Add the macro name to the stream. 411a267692SJohannes Altmanninger printMacroName(MacroStackStream, Context, Loc); 421a267692SJohannes Altmanninger Loc = SM.getImmediateMacroCallerLoc(Loc); 431a267692SJohannes Altmanninger } 441a267692SJohannes Altmanninger return MacroStack; 451a267692SJohannes Altmanninger } 461a267692SJohannes Altmanninger 471a267692SJohannes Altmanninger } // end namespace data_collection 481a267692SJohannes Altmanninger } // end namespace clang 49