Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/PDB/PDBSymbolCompiland.cpp
35269 views
//===- PDBSymbolCompiland.cpp - compiland details ---------------*- 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 "llvm/DebugInfo/PDB/IPDBSession.h"9#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"1011#include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"12#include "llvm/DebugInfo/PDB/PDBSymDumper.h"13#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"14#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"15#include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h"1617#include "llvm/ADT/StringSwitch.h"18#include "llvm/Support/Path.h"19#include <utility>2021using namespace llvm;22using namespace llvm::pdb;2324void PDBSymbolCompiland::dump(PDBSymDumper &Dumper) const {25Dumper.dump(*this);26}2728std::string PDBSymbolCompiland::getSourceFileName() const {29return sys::path::filename(getSourceFileFullPath()).str();30}3132std::string PDBSymbolCompiland::getSourceFileFullPath() const {33std::string SourceFileFullPath;3435// RecordedResult could be the basename, relative path or full path of the36// source file. Usually it is retrieved and recorded from the command that37// compiles this compiland.38//39// cmd FileName -> RecordedResult = .\\FileName40// cmd (Path)\\FileName -> RecordedResult = (Path)\\FileName41//42std::string RecordedResult = RawSymbol->getSourceFileName();4344if (RecordedResult.empty()) {45if (auto Envs = findAllChildren<PDBSymbolCompilandEnv>()) {46std::string EnvWorkingDir, EnvSrc;4748while (auto Env = Envs->getNext()) {49std::string Var = Env->getName();50if (Var == "cwd") {51EnvWorkingDir = Env->getValue();52continue;53}54if (Var == "src") {55EnvSrc = Env->getValue();56if (sys::path::is_absolute(EnvSrc))57return EnvSrc;58RecordedResult = EnvSrc;59continue;60}61}62if (!EnvWorkingDir.empty() && !EnvSrc.empty()) {63auto Len = EnvWorkingDir.length();64if (EnvWorkingDir[Len - 1] != '/' && EnvWorkingDir[Len - 1] != '\\') {65std::string Path = EnvWorkingDir + "\\" + EnvSrc;66std::replace(Path.begin(), Path.end(), '/', '\\');67// We will return it as full path if we can't find a better one.68if (sys::path::is_absolute(Path))69SourceFileFullPath = Path;70}71}72}73}7475if (!RecordedResult.empty()) {76if (sys::path::is_absolute(RecordedResult))77return RecordedResult;7879// This searches name that has same basename as the one in RecordedResult.80auto OneSrcFile = Session.findOneSourceFile(81this, RecordedResult, PDB_NameSearchFlags::NS_CaseInsensitive);82if (OneSrcFile)83return OneSrcFile->getFileName();84}8586// At this point, we have to walk through all source files of this compiland,87// and determine the right source file if any that is used to generate this88// compiland based on language indicated in compilanddetails language field.89auto Details = findOneChild<PDBSymbolCompilandDetails>();90PDB_Lang Lang = Details ? Details->getLanguage() : PDB_Lang::Cpp;91auto SrcFiles = Session.getSourceFilesForCompiland(*this);92if (SrcFiles) {93while (auto File = SrcFiles->getNext()) {94std::string FileName = File->getFileName();95auto file_extension = sys::path::extension(FileName);96if (StringSwitch<bool>(file_extension.lower())97.Case(".cpp", Lang == PDB_Lang::Cpp)98.Case(".cc", Lang == PDB_Lang::Cpp)99.Case(".cxx", Lang == PDB_Lang::Cpp)100.Case(".c", Lang == PDB_Lang::C)101.Case(".asm", Lang == PDB_Lang::Masm)102.Case(".swift", Lang == PDB_Lang::Swift)103.Case(".rs", Lang == PDB_Lang::Rust)104.Case(".m", Lang == PDB_Lang::ObjC)105.Case(".mm", Lang == PDB_Lang::ObjCpp)106.Default(false))107return File->getFileName();108}109}110111return SourceFileFullPath;112}113114115