1c0a5c01aSAaron Smith //===- PDBSymbolCompiland.cpp - compiland details ---------------*- C++ -*-===// 221473f7bSZachary Turner // 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 621473f7bSZachary Turner // 721473f7bSZachary Turner //===----------------------------------------------------------------------===// 821473f7bSZachary Turner 9da611207SAaron Smith #include "llvm/DebugInfo/PDB/IPDBSession.h" 10da611207SAaron Smith #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 11da611207SAaron Smith 1252c9f881SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 13da611207SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 14c0a5c01aSAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h" 159a818ad1SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymDumper.h" 1652c9f881SZachary Turner 17da611207SAaron Smith #include "llvm/ADT/StringSwitch.h" 18da611207SAaron Smith #include "llvm/Support/Path.h" 1971f308adSChandler Carruth #include <utility> 2021473f7bSZachary Turner 2121473f7bSZachary Turner using namespace llvm; 22ec28fc34SZachary Turner using namespace llvm::pdb; 2321473f7bSZachary Turner 24b52d08d9SZachary Turner void PDBSymbolCompiland::dump(PDBSymDumper &Dumper) const { 25b52d08d9SZachary Turner Dumper.dump(*this); 2621473f7bSZachary Turner } 2743ec3af9SZachary Turner 28da611207SAaron Smith std::string PDBSymbolCompiland::getSourceFileName() const { 29c0a5c01aSAaron Smith return sys::path::filename(getSourceFileFullPath()).str(); 30da611207SAaron Smith } 31da611207SAaron Smith 32da611207SAaron Smith std::string PDBSymbolCompiland::getSourceFileFullPath() const { 33da611207SAaron Smith std::string SourceFileFullPath; 34da611207SAaron Smith 35da611207SAaron Smith // RecordedResult could be the basename, relative path or full path of the 36da611207SAaron Smith // source file. Usually it is retrieved and recorded from the command that 37da611207SAaron Smith // compiles this compiland. 38da611207SAaron Smith // 39da611207SAaron Smith // cmd FileName -> RecordedResult = .\\FileName 40da611207SAaron Smith // cmd (Path)\\FileName -> RecordedResult = (Path)\\FileName 41da611207SAaron Smith // 42da611207SAaron Smith std::string RecordedResult = RawSymbol->getSourceFileName(); 43da611207SAaron Smith 44da611207SAaron Smith if (RecordedResult.empty()) { 45da611207SAaron Smith if (auto Envs = findAllChildren<PDBSymbolCompilandEnv>()) { 46da611207SAaron Smith std::string EnvWorkingDir, EnvSrc; 47da611207SAaron Smith 4843ec3af9SZachary Turner while (auto Env = Envs->getNext()) { 4943ec3af9SZachary Turner std::string Var = Env->getName(); 50da611207SAaron Smith if (Var == "cwd") { 51da611207SAaron Smith EnvWorkingDir = Env->getValue(); 5243ec3af9SZachary Turner continue; 5343ec3af9SZachary Turner } 54da611207SAaron Smith if (Var == "src") { 55da611207SAaron Smith EnvSrc = Env->getValue(); 56c0a5c01aSAaron Smith if (sys::path::is_absolute(EnvSrc)) 57da611207SAaron Smith return EnvSrc; 58da611207SAaron Smith RecordedResult = EnvSrc; 59da611207SAaron Smith continue; 60da611207SAaron Smith } 61da611207SAaron Smith } 62da611207SAaron Smith if (!EnvWorkingDir.empty() && !EnvSrc.empty()) { 63da611207SAaron Smith auto Len = EnvWorkingDir.length(); 64da611207SAaron Smith if (EnvWorkingDir[Len - 1] != '/' && EnvWorkingDir[Len - 1] != '\\') { 65da611207SAaron Smith std::string Path = EnvWorkingDir + "\\" + EnvSrc; 66da611207SAaron Smith std::replace(Path.begin(), Path.end(), '/', '\\'); 67da611207SAaron Smith // We will return it as full path if we can't find a better one. 68c0a5c01aSAaron Smith if (sys::path::is_absolute(Path)) 69da611207SAaron Smith SourceFileFullPath = Path; 70da611207SAaron Smith } 71da611207SAaron Smith } 72da611207SAaron Smith } 73da611207SAaron Smith } 74da611207SAaron Smith 75da611207SAaron Smith if (!RecordedResult.empty()) { 76c0a5c01aSAaron Smith if (sys::path::is_absolute(RecordedResult)) 77da611207SAaron Smith return RecordedResult; 78da611207SAaron Smith 79da611207SAaron Smith // This searches name that has same basename as the one in RecordedResult. 80c0a5c01aSAaron Smith auto OneSrcFile = Session.findOneSourceFile( 81c0a5c01aSAaron Smith this, RecordedResult, PDB_NameSearchFlags::NS_CaseInsensitive); 82da611207SAaron Smith if (OneSrcFile) 83da611207SAaron Smith return OneSrcFile->getFileName(); 84da611207SAaron Smith } 85da611207SAaron Smith 86da611207SAaron Smith // At this point, we have to walk through all source files of this compiland, 87da611207SAaron Smith // and determine the right source file if any that is used to generate this 88da611207SAaron Smith // compiland based on language indicated in compilanddetails language field. 89da611207SAaron Smith auto Details = findOneChild<PDBSymbolCompilandDetails>(); 90da611207SAaron Smith PDB_Lang Lang = Details ? Details->getLanguage() : PDB_Lang::Cpp; 91da611207SAaron Smith auto SrcFiles = Session.getSourceFilesForCompiland(*this); 92da611207SAaron Smith if (SrcFiles) { 93da611207SAaron Smith while (auto File = SrcFiles->getNext()) { 94da611207SAaron Smith std::string FileName = File->getFileName(); 95c0a5c01aSAaron Smith auto file_extension = sys::path::extension(FileName); 96da611207SAaron Smith if (StringSwitch<bool>(file_extension.lower()) 97*cc51dc64SNathan Lanza .Case(".cpp", Lang == PDB_Lang::Cpp) 98*cc51dc64SNathan Lanza .Case(".cc", Lang == PDB_Lang::Cpp) 99*cc51dc64SNathan Lanza .Case(".cxx", Lang == PDB_Lang::Cpp) 100*cc51dc64SNathan Lanza .Case(".c", Lang == PDB_Lang::C) 101da611207SAaron Smith .Case(".asm", Lang == PDB_Lang::Masm) 102*cc51dc64SNathan Lanza .Case(".swift", Lang == PDB_Lang::Swift) 103da611207SAaron Smith .Default(false)) 104da611207SAaron Smith return File->getFileName(); 105da611207SAaron Smith } 106da611207SAaron Smith } 107da611207SAaron Smith 108da611207SAaron Smith return SourceFileFullPath; 10943ec3af9SZachary Turner } 110