34
34
#include " swift/SIL/SILDebugScope.h"
35
35
#include " swift/SIL/SILModule.h"
36
36
#include " clang/AST/ASTContext.h"
37
+ #include " clang/AST/ExternalASTSource.h"
37
38
#include " clang/AST/Decl.h"
38
39
#include " clang/Basic/Module.h"
39
40
#include " clang/Basic/SourceLocation.h"
40
41
#include " clang/Basic/SourceManager.h"
41
42
#include " clang/Basic/TargetInfo.h"
43
+ #include " clang/Frontend/CompilerInstance.h"
44
+ #include " clang/Serialization/ASTReader.h"
42
45
#include " llvm/Config/config.h"
43
46
#include " llvm/IR/DebugInfo.h"
44
47
#include " llvm/IR/Module.h"
@@ -190,7 +193,7 @@ SILLocation::DebugLoc getDeserializedLoc(Decl *D) {
190
193
// / Use the SM to figure out the actual line/column of a SourceLoc.
191
194
template <typename WithLoc>
192
195
SILLocation::DebugLoc getDebugLoc (IRGenDebugInfo &DI, WithLoc *S,
193
- bool End = false ) {
196
+ bool End = false ) {
194
197
SILLocation::DebugLoc L;
195
198
if (S == nullptr )
196
199
return L;
@@ -814,6 +817,23 @@ llvm::DIModule *IRGenDebugInfo::getOrCreateModule(StringRef Key,
814
817
return M;
815
818
}
816
819
820
+ llvm::DIModule *IRGenDebugInfo::getOrCreateModule (
821
+ clang::ExternalASTSource::ASTSourceDescriptor Desc) {
822
+ // Handle Clang modules.
823
+ if (const clang::Module *ClangModule = Desc.getModuleOrNull ()) {
824
+ llvm::DIModule *Parent = nullptr ;
825
+ if (ClangModule->Parent ) {
826
+ clang::ExternalASTSource::ASTSourceDescriptor PM (*ClangModule->Parent );
827
+ Parent = getOrCreateModule (PM);
828
+ }
829
+ return getOrCreateModule (ClangModule->getFullModuleName (), Parent,
830
+ Desc.getModuleName (), Desc.getPath ());
831
+ }
832
+ // Handle PCH.
833
+ return getOrCreateModule (Desc.getASTFile (), nullptr , Desc.getModuleName (),
834
+ Desc.getPath ());
835
+ };
836
+
817
837
llvm::DISubprogram *IRGenDebugInfo::emitFunction (SILFunction &SILFn,
818
838
llvm::Function *Fn) {
819
839
auto *DS = SILFn.getDebugScope ();
@@ -1491,6 +1511,13 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
1491
1511
auto *StructTy = BaseTy->castTo <StructType>();
1492
1512
auto *Decl = StructTy->getDecl ();
1493
1513
auto L = getDebugLoc (*this , Decl);
1514
+ if (auto *ClangDecl = Decl->getClangDecl ()) {
1515
+ auto ClangSrcLoc = ClangDecl->getLocStart ();
1516
+ clang::SourceManager &ClangSM =
1517
+ CI.getClangASTContext ().getSourceManager ();
1518
+ L.Line = ClangSM.getPresumedLineNumber (ClangSrcLoc);
1519
+ L.Filename = ClangSM.getBufferName (ClangSrcLoc);
1520
+ }
1494
1521
auto *File = getOrCreateFile (L.Filename );
1495
1522
if (Opts.DebugInfoKind > IRGenDebugInfoKind::ASTTypes)
1496
1523
return createStructType (DbgTy, Decl, StructTy, Scope, File, L.Line ,
@@ -1515,28 +1542,6 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
1515
1542
CI.getClangASTContext ().getSourceManager ();
1516
1543
L.Line = ClangSM.getPresumedLineNumber (ClangSrcLoc);
1517
1544
L.Filename = ClangSM.getBufferName (ClangSrcLoc);
1518
-
1519
- // Use "__ObjC" as default for implicit decls.
1520
- // FIXME: Do something more clever based on the decl's mangled name.
1521
- std::string FullModuleNameBuffer;
1522
- StringRef ModulePath;
1523
- StringRef ModuleName = " __ObjC" ;
1524
- if (auto *OwningModule = ClangDecl->getImportedOwningModule ())
1525
- ModuleName = OwningModule->getTopLevelModuleName ();
1526
-
1527
- if (auto *SwiftModule = Decl->getParentModule ())
1528
- if (auto *ClangModule = SwiftModule->findUnderlyingClangModule ()) {
1529
- // FIXME: Clang submodules are not handled here.
1530
- // FIXME: Clang module config macros are not handled here.
1531
- FullModuleNameBuffer = ClangModule->getFullModuleName ();
1532
- ModuleName = FullModuleNameBuffer;
1533
- // FIXME: A clang module's Directory is supposed to be the
1534
- // directory containing the module map, but ClangImporter
1535
- // sets it to the module cache directory.
1536
- if (ClangModule->Directory )
1537
- ModulePath = ClangModule->Directory ->getName ();
1538
- }
1539
- Scope = getOrCreateModule (ModuleName, TheCU, ModuleName, ModulePath);
1540
1545
}
1541
1546
assert (SizeInBits == CI.getTargetInfo ().getPointerWidth (0 ));
1542
1547
return createPointerSizedStruct (Scope, Decl->getNameStr (),
@@ -1856,10 +1861,20 @@ llvm::DIType *IRGenDebugInfo::getOrCreateType(DebugTypeInfo DbgTy) {
1856
1861
//
1857
1862
// FIXME: Builtin and qualified types in LLVM have no parent
1858
1863
// scope. TODO: This can be fixed by extending DIBuilder.
1864
+ llvm::DIScope *Scope = nullptr ;
1859
1865
DeclContext *Context = DbgTy.getType ()->getNominalOrBoundGenericNominal ();
1860
- if (Context)
1866
+ if (Context) {
1867
+ if (auto *D = Context->getAsNominalTypeOrNominalTypeExtensionContext ())
1868
+ if (auto *ClangDecl = D->getClangDecl ()) {
1869
+ clang::ASTReader &Reader = *CI.getClangInstance ().getModuleManager ();
1870
+ auto Idx = ClangDecl->getOwningModuleID ();
1871
+ if (auto Info = Reader.getSourceDescriptor (Idx))
1872
+ Scope = getOrCreateModule (*Info);
1873
+ }
1861
1874
Context = Context->getParent ();
1862
- llvm::DIScope *Scope = getOrCreateContext (Context);
1875
+ }
1876
+ if (!Scope)
1877
+ Scope = getOrCreateContext (Context);
1863
1878
llvm::DIType *DITy = createType (DbgTy, MangledName, Scope, getFile (Scope));
1864
1879
1865
1880
// Incrementally build the DIRefMap.
0 commit comments