1//===- PDBSymbolCompiland.cpp - compiland details ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/PDB/IPDBSession.h"
10#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
11
12#include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
13#include "llvm/DebugInfo/PDB/PDBSymDumper.h"
14#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
15#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
16#include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h"
17
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/Support/Path.h"
20#include <utility>
21
22using namespace llvm;
23using namespace llvm::pdb;
24
25void PDBSymbolCompiland::dump(PDBSymDumper &Dumper) const {
26 Dumper.dump(Symbol: *this);
27}
28
29std::string PDBSymbolCompiland::getSourceFileName() const {
30 return sys::path::filename(path: getSourceFileFullPath()).str();
31}
32
33std::string PDBSymbolCompiland::getSourceFileFullPath() const {
34 std::string SourceFileFullPath;
35
36 // RecordedResult could be the basename, relative path or full path of the
37 // source file. Usually it is retrieved and recorded from the command that
38 // compiles this compiland.
39 //
40 // cmd FileName -> RecordedResult = .\\FileName
41 // cmd (Path)\\FileName -> RecordedResult = (Path)\\FileName
42 //
43 std::string RecordedResult = RawSymbol->getSourceFileName();
44
45 if (RecordedResult.empty()) {
46 if (auto Envs = findAllChildren<PDBSymbolCompilandEnv>()) {
47 std::string EnvWorkingDir, EnvSrc;
48
49 while (auto Env = Envs->getNext()) {
50 std::string Var = Env->getName();
51 if (Var == "cwd") {
52 EnvWorkingDir = Env->getValue();
53 continue;
54 }
55 if (Var == "src") {
56 EnvSrc = Env->getValue();
57 if (sys::path::is_absolute(path: EnvSrc))
58 return EnvSrc;
59 RecordedResult = EnvSrc;
60 continue;
61 }
62 }
63 if (!EnvWorkingDir.empty() && !EnvSrc.empty()) {
64 auto Len = EnvWorkingDir.length();
65 if (EnvWorkingDir[Len - 1] != '/' && EnvWorkingDir[Len - 1] != '\\') {
66 std::string Path = EnvWorkingDir + "\\" + EnvSrc;
67 std::replace(first: Path.begin(), last: Path.end(), old_value: '/', new_value: '\\');
68 // We will return it as full path if we can't find a better one.
69 if (sys::path::is_absolute(path: Path))
70 SourceFileFullPath = Path;
71 }
72 }
73 }
74 }
75
76 if (!RecordedResult.empty()) {
77 if (sys::path::is_absolute(path: RecordedResult))
78 return RecordedResult;
79
80 // This searches name that has same basename as the one in RecordedResult.
81 auto OneSrcFile = Session.findOneSourceFile(
82 Compiland: this, Pattern: RecordedResult, Flags: PDB_NameSearchFlags::NS_CaseInsensitive);
83 if (OneSrcFile)
84 return OneSrcFile->getFileName();
85 }
86
87 // At this point, we have to walk through all source files of this compiland,
88 // and determine the right source file if any that is used to generate this
89 // compiland based on language indicated in compilanddetails language field.
90 auto Details = findOneChild<PDBSymbolCompilandDetails>();
91 PDB_Lang Lang = Details ? Details->getLanguage() : PDB_Lang::Cpp;
92 auto SrcFiles = Session.getSourceFilesForCompiland(Compiland: *this);
93 if (SrcFiles) {
94 while (auto File = SrcFiles->getNext()) {
95 std::string FileName = File->getFileName();
96 auto file_extension = sys::path::extension(path: FileName);
97 if (StringSwitch<bool>(file_extension.lower())
98 .Case(S: ".cpp", Value: Lang == PDB_Lang::Cpp)
99 .Case(S: ".cc", Value: Lang == PDB_Lang::Cpp)
100 .Case(S: ".cxx", Value: Lang == PDB_Lang::Cpp)
101 .Case(S: ".c", Value: Lang == PDB_Lang::C)
102 .Case(S: ".asm", Value: Lang == PDB_Lang::Masm)
103 .Case(S: ".swift", Value: Lang == PDB_Lang::Swift)
104 .Case(S: ".rs", Value: Lang == PDB_Lang::Rust)
105 .Case(S: ".m", Value: Lang == PDB_Lang::ObjC)
106 .Case(S: ".mm", Value: Lang == PDB_Lang::ObjCpp)
107 .Default(Value: false))
108 return File->getFileName();
109 }
110 }
111
112 return SourceFileFullPath;
113}
114