10b57cec5SDimitry Andric //===-- DataCollection.cpp --------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "clang/AST/DataCollection.h"
10*5ffd83dbSDimitry Andric #include "clang/Basic/SourceManager.h"
110b57cec5SDimitry Andric #include "clang/Lex/Lexer.h"
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric namespace clang {
140b57cec5SDimitry Andric namespace data_collection {
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric /// Prints the macro name that contains the given SourceLocation into the given
170b57cec5SDimitry Andric /// raw_string_ostream.
printMacroName(llvm::raw_string_ostream & MacroStack,ASTContext & Context,SourceLocation Loc)180b57cec5SDimitry Andric static void printMacroName(llvm::raw_string_ostream &MacroStack,
190b57cec5SDimitry Andric ASTContext &Context, SourceLocation Loc) {
200b57cec5SDimitry Andric MacroStack << Lexer::getImmediateMacroName(Loc, Context.getSourceManager(),
210b57cec5SDimitry Andric Context.getLangOpts());
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric // Add an empty space at the end as a padding to prevent
240b57cec5SDimitry Andric // that macro names concatenate to the names of other macros.
250b57cec5SDimitry Andric MacroStack << " ";
260b57cec5SDimitry Andric }
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric /// Returns a string that represents all macro expansions that expanded into the
290b57cec5SDimitry Andric /// given SourceLocation.
300b57cec5SDimitry Andric ///
310b57cec5SDimitry Andric /// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations
320b57cec5SDimitry Andric /// A and B are expanded from the same macros in the same order.
getMacroStack(SourceLocation Loc,ASTContext & Context)330b57cec5SDimitry Andric std::string getMacroStack(SourceLocation Loc, ASTContext &Context) {
340b57cec5SDimitry Andric std::string MacroStack;
350b57cec5SDimitry Andric llvm::raw_string_ostream MacroStackStream(MacroStack);
360b57cec5SDimitry Andric SourceManager &SM = Context.getSourceManager();
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric // Iterate over all macros that expanded into the given SourceLocation.
390b57cec5SDimitry Andric while (Loc.isMacroID()) {
400b57cec5SDimitry Andric // Add the macro name to the stream.
410b57cec5SDimitry Andric printMacroName(MacroStackStream, Context, Loc);
420b57cec5SDimitry Andric Loc = SM.getImmediateMacroCallerLoc(Loc);
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric MacroStackStream.flush();
450b57cec5SDimitry Andric return MacroStack;
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric } // end namespace data_collection
490b57cec5SDimitry Andric } // end namespace clang
50