PDBSymbolCompiland.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "llvm/DebugInfo/PDB/IPDBSession.h"
  9. #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
  10. #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
  11. #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
  12. #include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h"
  13. #include "llvm/DebugInfo/PDB/PDBSymDumper.h"
  14. #include "llvm/ADT/StringSwitch.h"
  15. #include "llvm/Support/Path.h"
  16. #include <utility>
  17. using namespace llvm;
  18. using namespace llvm::pdb;
  19. void PDBSymbolCompiland::dump(PDBSymDumper &Dumper) const {
  20. Dumper.dump(*this);
  21. }
  22. std::string PDBSymbolCompiland::getSourceFileName() const {
  23. return sys::path::filename(getSourceFileFullPath()).str();
  24. }
  25. std::string PDBSymbolCompiland::getSourceFileFullPath() const {
  26. std::string SourceFileFullPath;
  27. // RecordedResult could be the basename, relative path or full path of the
  28. // source file. Usually it is retrieved and recorded from the command that
  29. // compiles this compiland.
  30. //
  31. // cmd FileName -> RecordedResult = .\\FileName
  32. // cmd (Path)\\FileName -> RecordedResult = (Path)\\FileName
  33. //
  34. std::string RecordedResult = RawSymbol->getSourceFileName();
  35. if (RecordedResult.empty()) {
  36. if (auto Envs = findAllChildren<PDBSymbolCompilandEnv>()) {
  37. std::string EnvWorkingDir, EnvSrc;
  38. while (auto Env = Envs->getNext()) {
  39. std::string Var = Env->getName();
  40. if (Var == "cwd") {
  41. EnvWorkingDir = Env->getValue();
  42. continue;
  43. }
  44. if (Var == "src") {
  45. EnvSrc = Env->getValue();
  46. if (sys::path::is_absolute(EnvSrc))
  47. return EnvSrc;
  48. RecordedResult = EnvSrc;
  49. continue;
  50. }
  51. }
  52. if (!EnvWorkingDir.empty() && !EnvSrc.empty()) {
  53. auto Len = EnvWorkingDir.length();
  54. if (EnvWorkingDir[Len - 1] != '/' && EnvWorkingDir[Len - 1] != '\\') {
  55. std::string Path = EnvWorkingDir + "\\" + EnvSrc;
  56. std::replace(Path.begin(), Path.end(), '/', '\\');
  57. // We will return it as full path if we can't find a better one.
  58. if (sys::path::is_absolute(Path))
  59. SourceFileFullPath = Path;
  60. }
  61. }
  62. }
  63. }
  64. if (!RecordedResult.empty()) {
  65. if (sys::path::is_absolute(RecordedResult))
  66. return RecordedResult;
  67. // This searches name that has same basename as the one in RecordedResult.
  68. auto OneSrcFile = Session.findOneSourceFile(
  69. this, RecordedResult, PDB_NameSearchFlags::NS_CaseInsensitive);
  70. if (OneSrcFile)
  71. return OneSrcFile->getFileName();
  72. }
  73. // At this point, we have to walk through all source files of this compiland,
  74. // and determine the right source file if any that is used to generate this
  75. // compiland based on language indicated in compilanddetails language field.
  76. auto Details = findOneChild<PDBSymbolCompilandDetails>();
  77. PDB_Lang Lang = Details ? Details->getLanguage() : PDB_Lang::Cpp;
  78. auto SrcFiles = Session.getSourceFilesForCompiland(*this);
  79. if (SrcFiles) {
  80. while (auto File = SrcFiles->getNext()) {
  81. std::string FileName = File->getFileName();
  82. auto file_extension = sys::path::extension(FileName);
  83. if (StringSwitch<bool>(file_extension.lower())
  84. .Case(".cpp", Lang == PDB_Lang::Cpp)
  85. .Case(".cc", Lang == PDB_Lang::Cpp)
  86. .Case(".cxx", Lang == PDB_Lang::Cpp)
  87. .Case(".c", Lang == PDB_Lang::C)
  88. .Case(".asm", Lang == PDB_Lang::Masm)
  89. .Case(".swift", Lang == PDB_Lang::Swift)
  90. .Default(false))
  91. return File->getFileName();
  92. }
  93. }
  94. return SourceFileFullPath;
  95. }