blob: cea53a9b734764f0f95a6230ea214420bb8057e8 [file] [log] [blame]
Zachary Turner21473f72015-02-08 00:29:29 +00001//===- 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 Smithda611202018-03-19 21:20:04 +000010#include "llvm/DebugInfo/PDB/IPDBSession.h"
11#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
12
Zachary Turner52c9f882015-02-14 03:53:56 +000013#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turner43ec3af2016-02-18 18:47:29 +000014#include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h"
Aaron Smithda611202018-03-19 21:20:04 +000015#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000016#include "llvm/DebugInfo/PDB/PDBSymDumper.h"
Zachary Turner52c9f882015-02-14 03:53:56 +000017
Aaron Smithda611202018-03-19 21:20:04 +000018#include "llvm/ADT/StringSwitch.h"
19#include "llvm/Support/Path.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000020#include <utility>
Zachary Turner21473f72015-02-08 00:29:29 +000021
22using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000023using namespace llvm::pdb;
Zachary Turner21473f72015-02-08 00:29:29 +000024
Zachary Turner98571ed2015-02-08 22:53:53 +000025PDBSymbolCompiland::PDBSymbolCompiland(const IPDBSession &PDBSession,
Zachary Turnerbae16b32015-02-08 20:58:09 +000026 std::unique_ptr<IPDBRawSymbol> Symbol)
Zachary Turner1b1a70f2017-04-10 06:14:09 +000027 : PDBSymbol(PDBSession, std::move(Symbol)) {
28 assert(RawSymbol->getSymTag() == PDB_SymType::Compiland);
29}
Zachary Turner21473f72015-02-08 00:29:29 +000030
Zachary Turnerb52d08d2015-03-01 06:51:29 +000031void PDBSymbolCompiland::dump(PDBSymDumper &Dumper) const {
32 Dumper.dump(*this);
Zachary Turner21473f72015-02-08 00:29:29 +000033}
Zachary Turner43ec3af2016-02-18 18:47:29 +000034
Aaron Smithda611202018-03-19 21:20:04 +000035std::string PDBSymbolCompiland::getSourceFileName() const {
36 return llvm::sys::path::filename(getSourceFileFullPath()).str();
37}
38
39std::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 Turner43ec3af2016-02-18 18:47:29 +000056 std::string Var = Env->getName();
Aaron Smithda611202018-03-19 21:20:04 +000057 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 Turner43ec3af2016-02-18 18:47:29 +000079 }
Aaron Smithda611202018-03-19 21:20:04 +000080 }
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 Turner43ec3af2016-02-18 18:47:29 +0000117}