Path: blob/main/contrib/llvm-project/clang/lib/AST/DataCollection.cpp
35260 views
//===-- DataCollection.cpp --------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/AST/DataCollection.h"9#include "clang/Basic/SourceManager.h"10#include "clang/Lex/Lexer.h"1112namespace clang {13namespace data_collection {1415/// Prints the macro name that contains the given SourceLocation into the given16/// raw_string_ostream.17static void printMacroName(llvm::raw_string_ostream &MacroStack,18ASTContext &Context, SourceLocation Loc) {19MacroStack << Lexer::getImmediateMacroName(Loc, Context.getSourceManager(),20Context.getLangOpts());2122// Add an empty space at the end as a padding to prevent23// that macro names concatenate to the names of other macros.24MacroStack << " ";25}2627/// Returns a string that represents all macro expansions that expanded into the28/// given SourceLocation.29///30/// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations31/// A and B are expanded from the same macros in the same order.32std::string getMacroStack(SourceLocation Loc, ASTContext &Context) {33std::string MacroStack;34llvm::raw_string_ostream MacroStackStream(MacroStack);35SourceManager &SM = Context.getSourceManager();3637// Iterate over all macros that expanded into the given SourceLocation.38while (Loc.isMacroID()) {39// Add the macro name to the stream.40printMacroName(MacroStackStream, Context, Loc);41Loc = SM.getImmediateMacroCallerLoc(Loc);42}43MacroStackStream.flush();44return MacroStack;45}4647} // end namespace data_collection48} // end namespace clang495051