10b57cec5SDimitry Andric //===- PDBSymbolCompiland.cpp - compiland details ---------------*- 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 "llvm/DebugInfo/PDB/IPDBSession.h" 100b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 130b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 140b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h" 150b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymDumper.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h" 180b57cec5SDimitry Andric #include "llvm/Support/Path.h" 190b57cec5SDimitry Andric #include <utility> 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace llvm; 220b57cec5SDimitry Andric using namespace llvm::pdb; 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric void PDBSymbolCompiland::dump(PDBSymDumper &Dumper) const { 250b57cec5SDimitry Andric Dumper.dump(*this); 260b57cec5SDimitry Andric } 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric std::string PDBSymbolCompiland::getSourceFileName() const { 290b57cec5SDimitry Andric return sys::path::filename(getSourceFileFullPath()).str(); 300b57cec5SDimitry Andric } 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric std::string PDBSymbolCompiland::getSourceFileFullPath() const { 330b57cec5SDimitry Andric std::string SourceFileFullPath; 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric // RecordedResult could be the basename, relative path or full path of the 360b57cec5SDimitry Andric // source file. Usually it is retrieved and recorded from the command that 370b57cec5SDimitry Andric // compiles this compiland. 380b57cec5SDimitry Andric // 390b57cec5SDimitry Andric // cmd FileName -> RecordedResult = .\\FileName 400b57cec5SDimitry Andric // cmd (Path)\\FileName -> RecordedResult = (Path)\\FileName 410b57cec5SDimitry Andric // 420b57cec5SDimitry Andric std::string RecordedResult = RawSymbol->getSourceFileName(); 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric if (RecordedResult.empty()) { 450b57cec5SDimitry Andric if (auto Envs = findAllChildren<PDBSymbolCompilandEnv>()) { 460b57cec5SDimitry Andric std::string EnvWorkingDir, EnvSrc; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric while (auto Env = Envs->getNext()) { 490b57cec5SDimitry Andric std::string Var = Env->getName(); 500b57cec5SDimitry Andric if (Var == "cwd") { 510b57cec5SDimitry Andric EnvWorkingDir = Env->getValue(); 520b57cec5SDimitry Andric continue; 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric if (Var == "src") { 550b57cec5SDimitry Andric EnvSrc = Env->getValue(); 560b57cec5SDimitry Andric if (sys::path::is_absolute(EnvSrc)) 570b57cec5SDimitry Andric return EnvSrc; 580b57cec5SDimitry Andric RecordedResult = EnvSrc; 590b57cec5SDimitry Andric continue; 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric if (!EnvWorkingDir.empty() && !EnvSrc.empty()) { 630b57cec5SDimitry Andric auto Len = EnvWorkingDir.length(); 640b57cec5SDimitry Andric if (EnvWorkingDir[Len - 1] != '/' && EnvWorkingDir[Len - 1] != '\\') { 650b57cec5SDimitry Andric std::string Path = EnvWorkingDir + "\\" + EnvSrc; 660b57cec5SDimitry Andric std::replace(Path.begin(), Path.end(), '/', '\\'); 670b57cec5SDimitry Andric // We will return it as full path if we can't find a better one. 680b57cec5SDimitry Andric if (sys::path::is_absolute(Path)) 690b57cec5SDimitry Andric SourceFileFullPath = Path; 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric if (!RecordedResult.empty()) { 760b57cec5SDimitry Andric if (sys::path::is_absolute(RecordedResult)) 770b57cec5SDimitry Andric return RecordedResult; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric // This searches name that has same basename as the one in RecordedResult. 800b57cec5SDimitry Andric auto OneSrcFile = Session.findOneSourceFile( 810b57cec5SDimitry Andric this, RecordedResult, PDB_NameSearchFlags::NS_CaseInsensitive); 820b57cec5SDimitry Andric if (OneSrcFile) 830b57cec5SDimitry Andric return OneSrcFile->getFileName(); 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric // At this point, we have to walk through all source files of this compiland, 870b57cec5SDimitry Andric // and determine the right source file if any that is used to generate this 880b57cec5SDimitry Andric // compiland based on language indicated in compilanddetails language field. 890b57cec5SDimitry Andric auto Details = findOneChild<PDBSymbolCompilandDetails>(); 900b57cec5SDimitry Andric PDB_Lang Lang = Details ? Details->getLanguage() : PDB_Lang::Cpp; 910b57cec5SDimitry Andric auto SrcFiles = Session.getSourceFilesForCompiland(*this); 920b57cec5SDimitry Andric if (SrcFiles) { 930b57cec5SDimitry Andric while (auto File = SrcFiles->getNext()) { 940b57cec5SDimitry Andric std::string FileName = File->getFileName(); 950b57cec5SDimitry Andric auto file_extension = sys::path::extension(FileName); 960b57cec5SDimitry Andric if (StringSwitch<bool>(file_extension.lower()) 970b57cec5SDimitry Andric .Case(".cpp", Lang == PDB_Lang::Cpp) 980b57cec5SDimitry Andric .Case(".cc", Lang == PDB_Lang::Cpp) 990b57cec5SDimitry Andric .Case(".cxx", Lang == PDB_Lang::Cpp) 1000b57cec5SDimitry Andric .Case(".c", Lang == PDB_Lang::C) 1010b57cec5SDimitry Andric .Case(".asm", Lang == PDB_Lang::Masm) 1020b57cec5SDimitry Andric .Case(".swift", Lang == PDB_Lang::Swift) 103*04eeddc0SDimitry Andric .Case(".rs", Lang == PDB_Lang::Rust) 1040b57cec5SDimitry Andric .Default(false)) 1050b57cec5SDimitry Andric return File->getFileName(); 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric return SourceFileFullPath; 1100b57cec5SDimitry Andric } 111