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