Zachary Turner | 21473f7 | 2015-02-08 00:29:29 +0000 | [diff] [blame] | 1 | //===- PDBSymbolCompiland.cpp - compiland details --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Aaron Smith | da61120 | 2018-03-19 21:20:04 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/IPDBSession.h" |
| 11 | #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" |
| 12 | |
Zachary Turner | 52c9f88 | 2015-02-14 03:53:56 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h" |
Aaron Smith | da61120 | 2018-03-19 21:20:04 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" |
Zachary Turner | 9a818ad | 2015-02-22 22:03:38 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/PDBSymDumper.h" |
Zachary Turner | 52c9f88 | 2015-02-14 03:53:56 +0000 | [diff] [blame] | 17 | |
Aaron Smith | da61120 | 2018-03-19 21:20:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSwitch.h" |
| 19 | #include "llvm/Support/Path.h" |
Chandler Carruth | 71f308a | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 20 | #include <utility> |
Zachary Turner | 21473f7 | 2015-02-08 00:29:29 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
Zachary Turner | ec28fc3 | 2016-05-04 20:32:13 +0000 | [diff] [blame] | 23 | using namespace llvm::pdb; |
Zachary Turner | 21473f7 | 2015-02-08 00:29:29 +0000 | [diff] [blame] | 24 | |
Zachary Turner | 98571ed | 2015-02-08 22:53:53 +0000 | [diff] [blame] | 25 | PDBSymbolCompiland::PDBSymbolCompiland(const IPDBSession &PDBSession, |
Zachary Turner | bae16b3 | 2015-02-08 20:58:09 +0000 | [diff] [blame] | 26 | std::unique_ptr<IPDBRawSymbol> Symbol) |
Zachary Turner | 1b1a70f | 2017-04-10 06:14:09 +0000 | [diff] [blame] | 27 | : PDBSymbol(PDBSession, std::move(Symbol)) { |
| 28 | assert(RawSymbol->getSymTag() == PDB_SymType::Compiland); |
| 29 | } |
Zachary Turner | 21473f7 | 2015-02-08 00:29:29 +0000 | [diff] [blame] | 30 | |
Zachary Turner | b52d08d | 2015-03-01 06:51:29 +0000 | [diff] [blame] | 31 | void PDBSymbolCompiland::dump(PDBSymDumper &Dumper) const { |
| 32 | Dumper.dump(*this); |
Zachary Turner | 21473f7 | 2015-02-08 00:29:29 +0000 | [diff] [blame] | 33 | } |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 34 | |
Aaron Smith | da61120 | 2018-03-19 21:20:04 +0000 | [diff] [blame] | 35 | std::string PDBSymbolCompiland::getSourceFileName() const { |
| 36 | return llvm::sys::path::filename(getSourceFileFullPath()).str(); |
| 37 | } |
| 38 | |
| 39 | std::string PDBSymbolCompiland::getSourceFileFullPath() const { |
| 40 | std::string SourceFileFullPath; |
| 41 | |
| 42 | // RecordedResult could be the basename, relative path or full path of the |
| 43 | // source file. Usually it is retrieved and recorded from the command that |
| 44 | // compiles this compiland. |
| 45 | // |
| 46 | // cmd FileName -> RecordedResult = .\\FileName |
| 47 | // cmd (Path)\\FileName -> RecordedResult = (Path)\\FileName |
| 48 | // |
| 49 | std::string RecordedResult = RawSymbol->getSourceFileName(); |
| 50 | |
| 51 | if (RecordedResult.empty()) { |
| 52 | if (auto Envs = findAllChildren<PDBSymbolCompilandEnv>()) { |
| 53 | std::string EnvWorkingDir, EnvSrc; |
| 54 | |
| 55 | while (auto Env = Envs->getNext()) { |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 56 | std::string Var = Env->getName(); |
Aaron Smith | da61120 | 2018-03-19 21:20:04 +0000 | [diff] [blame] | 57 | if (Var == "cwd") { |
| 58 | EnvWorkingDir = Env->getValue(); |
| 59 | continue; |
| 60 | } |
| 61 | if (Var == "src") { |
| 62 | EnvSrc = Env->getValue(); |
| 63 | if (llvm::sys::path::is_absolute(EnvSrc)) |
| 64 | return EnvSrc; |
| 65 | RecordedResult = EnvSrc; |
| 66 | continue; |
| 67 | } |
| 68 | } |
| 69 | if (!EnvWorkingDir.empty() && !EnvSrc.empty()) { |
| 70 | auto Len = EnvWorkingDir.length(); |
| 71 | if (EnvWorkingDir[Len - 1] != '/' && EnvWorkingDir[Len - 1] != '\\') { |
| 72 | std::string Path = EnvWorkingDir + "\\" + EnvSrc; |
| 73 | std::replace(Path.begin(), Path.end(), '/', '\\'); |
| 74 | // We will return it as full path if we can't find a better one. |
| 75 | if (llvm::sys::path::is_absolute(Path)) |
| 76 | SourceFileFullPath = Path; |
| 77 | } |
| 78 | } |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 79 | } |
Aaron Smith | da61120 | 2018-03-19 21:20:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (!RecordedResult.empty()) { |
| 83 | if (llvm::sys::path::is_absolute(RecordedResult)) |
| 84 | return RecordedResult; |
| 85 | |
| 86 | // This searches name that has same basename as the one in RecordedResult. |
| 87 | auto OneSrcFile = |
| 88 | Session.findOneSourceFile(this, RecordedResult, |
| 89 | PDB_NameSearchFlags::NS_CaseInsensitive); |
| 90 | if (OneSrcFile) |
| 91 | return OneSrcFile->getFileName(); |
| 92 | } |
| 93 | |
| 94 | // At this point, we have to walk through all source files of this compiland, |
| 95 | // and determine the right source file if any that is used to generate this |
| 96 | // compiland based on language indicated in compilanddetails language field. |
| 97 | auto Details = findOneChild<PDBSymbolCompilandDetails>(); |
| 98 | PDB_Lang Lang = Details ? Details->getLanguage() : PDB_Lang::Cpp; |
| 99 | auto SrcFiles = Session.getSourceFilesForCompiland(*this); |
| 100 | if (SrcFiles) { |
| 101 | bool LangC = (Lang == PDB_Lang::Cpp || Lang == PDB_Lang::C); |
| 102 | while (auto File = SrcFiles->getNext()) { |
| 103 | std::string FileName = File->getFileName(); |
| 104 | auto file_extension = llvm::sys::path::extension(FileName); |
| 105 | if (StringSwitch<bool>(file_extension.lower()) |
| 106 | .Case(".cpp", LangC) |
| 107 | .Case(".c", LangC) |
| 108 | .Case(".cc", LangC) |
| 109 | .Case(".cxx", LangC) |
| 110 | .Case(".asm", Lang == PDB_Lang::Masm) |
| 111 | .Default(false)) |
| 112 | return File->getFileName(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return SourceFileFullPath; |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 117 | } |