LLVM  16.0.0git
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 
17 
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/Support/Path.h"
20 #include <utility>
21 
22 using namespace llvm;
23 using namespace llvm::pdb;
24 
26  Dumper.dump(*this);
27 }
28 
31 }
32 
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(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(Path.begin(), Path.end(), '/', '\\');
68  // We will return it as full path if we can't find a better one.
70  SourceFileFullPath = Path;
71  }
72  }
73  }
74  }
75 
76  if (!RecordedResult.empty()) {
77  if (sys::path::is_absolute(RecordedResult))
78  return RecordedResult;
79 
80  // This searches name that has same basename as the one in RecordedResult.
81  auto OneSrcFile = Session.findOneSourceFile(
82  this, RecordedResult, 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(*this);
93  if (SrcFiles) {
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", Lang == PDB_Lang::Cpp)
99  .Case(".cc", Lang == PDB_Lang::Cpp)
100  .Case(".cxx", Lang == PDB_Lang::Cpp)
101  .Case(".c", Lang == PDB_Lang::C)
102  .Case(".asm", Lang == PDB_Lang::Masm)
103  .Case(".swift", Lang == PDB_Lang::Swift)
104  .Case(".rs", Lang == PDB_Lang::Rust)
105  .Default(false))
106  return File->getFileName();
107  }
108  }
109 
110  return SourceFileFullPath;
111 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
PDBSymbolCompilandEnv.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::sys::path::extension
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
Definition: Path.cpp:590
llvm::pdb::IPDBSession::findOneSourceFile
virtual std::unique_ptr< IPDBSourceFile > findOneSourceFile(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const =0
llvm::pdb::PDBSymbolCompiland::getSourceFileName
std::string getSourceFileName() const
Definition: PDBSymbolCompiland.cpp:29
Path.h
llvm::sys::path::is_absolute
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:671
PDBSymDumper.h
replace
static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New)
Definition: ConstantMerge.cpp:116
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::codeview::Masm
@ Masm
Definition: CodeView.h:147
llvm::pdb::PDBSymbolCompiland::dump
void dump(PDBSymDumper &Dumper) const override
Dumps the contents of a symbol a raw_ostream.
Definition: PDBSymbolCompiland.cpp:25
llvm::pdb::PDBSymDumper::dump
virtual void dump(const PDBSymbolAnnotation &Symbol)
Definition: PDBSymDumper.cpp:24
llvm::pdb::PDB_ColorItem::Path
@ Path
PDBSymbolCompilandDetails.h
llvm::pdb::IPDBSession::getSourceFilesForCompiland
virtual std::unique_ptr< IPDBEnumSourceFiles > getSourceFilesForCompiland(const PDBSymbolCompiland &Compiland) const =0
llvm::pdb
Definition: ConcreteSymbolEnumerator.h:20
IPDBSession.h
PDBSymbolCompiland.h
llvm::pdb::IPDBRawSymbol::getSourceFileName
virtual std::string getSourceFileName() const =0
llvm::pdb::PDBSymDumper
Definition: PDBSymDumper.h:19
llvm::pdb::PDBSymbol::Session
const IPDBSession & Session
Definition: PDBSymbol.h:168
llvm::codeview::SourceLanguage
SourceLanguage
These values correspond to the CV_CFL_LANG enumeration, and are documented here: https://msdn....
Definition: CodeView.h:143
llvm::pdb::PDBSymbol::RawSymbol
IPDBRawSymbol * RawSymbol
Definition: PDBSymbol.h:170
IPDBSourceFile.h
llvm::codeview::Rust
@ Rust
Definition: CodeView.h:162
llvm::pdb::PDBSymbolCompiland::getSourceFileFullPath
std::string getSourceFileFullPath() const
Definition: PDBSymbolCompiland.cpp:33
StringSwitch.h
llvm::codeview::Cpp
@ Cpp
Definition: CodeView.h:145
llvm::sys::path::filename
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:577
llvm::pdb::NS_CaseInsensitive
@ NS_CaseInsensitive
Definition: PDBTypes.h:105
llvm::StringSwitch::Default
R Default(T Value)
Definition: StringSwitch.h:183
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:45
llvm::StringRef::str
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:221
ConcreteSymbolEnumerator.h
llvm::codeview::Swift
@ Swift
Definition: CodeView.h:167