LLVM  9.0.0svn
PDBSymbolCompiland.cpp
Go to the documentation of this file.
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 
11 
16 
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/Support/Path.h"
19 #include <utility>
20 
21 using namespace llvm;
22 using namespace llvm::pdb;
23 
25  Dumper.dump(*this);
26 }
27 
30 }
31 
33  std::string SourceFileFullPath;
34 
35  // RecordedResult could be the basename, relative path or full path of the
36  // source file. Usually it is retrieved and recorded from the command that
37  // compiles this compiland.
38  //
39  // cmd FileName -> RecordedResult = .\\FileName
40  // cmd (Path)\\FileName -> RecordedResult = (Path)\\FileName
41  //
42  std::string RecordedResult = RawSymbol->getSourceFileName();
43 
44  if (RecordedResult.empty()) {
45  if (auto Envs = findAllChildren<PDBSymbolCompilandEnv>()) {
46  std::string EnvWorkingDir, EnvSrc;
47 
48  while (auto Env = Envs->getNext()) {
49  std::string Var = Env->getName();
50  if (Var == "cwd") {
51  EnvWorkingDir = Env->getValue();
52  continue;
53  }
54  if (Var == "src") {
55  EnvSrc = Env->getValue();
56  if (sys::path::is_absolute(EnvSrc))
57  return EnvSrc;
58  RecordedResult = EnvSrc;
59  continue;
60  }
61  }
62  if (!EnvWorkingDir.empty() && !EnvSrc.empty()) {
63  auto Len = EnvWorkingDir.length();
64  if (EnvWorkingDir[Len - 1] != '/' && EnvWorkingDir[Len - 1] != '\\') {
65  std::string Path = EnvWorkingDir + "\\" + EnvSrc;
66  std::replace(Path.begin(), Path.end(), '/', '\\');
67  // We will return it as full path if we can't find a better one.
68  if (sys::path::is_absolute(Path))
69  SourceFileFullPath = Path;
70  }
71  }
72  }
73  }
74 
75  if (!RecordedResult.empty()) {
76  if (sys::path::is_absolute(RecordedResult))
77  return RecordedResult;
78 
79  // This searches name that has same basename as the one in RecordedResult.
80  auto OneSrcFile = Session.findOneSourceFile(
81  this, RecordedResult, PDB_NameSearchFlags::NS_CaseInsensitive);
82  if (OneSrcFile)
83  return OneSrcFile->getFileName();
84  }
85 
86  // At this point, we have to walk through all source files of this compiland,
87  // and determine the right source file if any that is used to generate this
88  // compiland based on language indicated in compilanddetails language field.
89  auto Details = findOneChild<PDBSymbolCompilandDetails>();
90  PDB_Lang Lang = Details ? Details->getLanguage() : PDB_Lang::Cpp;
91  auto SrcFiles = Session.getSourceFilesForCompiland(*this);
92  if (SrcFiles) {
93  bool LangC = (Lang == PDB_Lang::Cpp || Lang == PDB_Lang::C);
94  while (auto File = SrcFiles->getNext()) {
95  std::string FileName = File->getFileName();
96  auto file_extension = sys::path::extension(FileName);
97  if (StringSwitch<bool>(file_extension.lower())
98  .Case(".cpp", LangC)
99  .Case(".c", LangC)
100  .Case(".cc", LangC)
101  .Case(".cxx", LangC)
102  .Case(".asm", Lang == PDB_Lang::Masm)
103  .Default(false))
104  return File->getFileName();
105  }
106  }
107 
108  return SourceFileFullPath;
109 }
uint64_t CallInst * C
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
IPDBRawSymbol * RawSymbol
Definition: PDBSymbol.h:165
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::string getSourceFileFullPath() const
void dump(PDBSymDumper &Dumper) const override
Dumps the contents of a symbol a raw_ostream.
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
virtual std::string getSourceFileName() const =0
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:687
virtual std::unique_ptr< IPDBEnumSourceFiles > getSourceFilesForCompiland(const PDBSymbolCompiland &Compiland) const =0
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New)
SourceLanguage
These values correspond to the CV_CFL_LANG enumeration, and are documented here: https://msdn.microsoft.com/en-us/library/bw3aekw6.aspx.
Definition: CodeView.h:143
virtual std::unique_ptr< IPDBSourceFile > findOneSourceFile(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const =0
std::string getSourceFileName() const
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:589
virtual void dump(const PDBSymbolAnnotation &Symbol)
const IPDBSession & Session
Definition: PDBSymbol.h:163
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
Definition: Path.cpp:604