1 //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is the source-level debug info generator for llvm translation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
16 
17 #include "CGBuilder.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/ExternalASTSource.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Frontend/CodeGenOptions.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/IR/DIBuilder.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include "llvm/Support/Allocator.h"
29 
30 namespace llvm {
31 class MDNode;
32 }
33 
34 namespace clang {
35 class CXXMethodDecl;
36 class ClassTemplateSpecializationDecl;
37 class GlobalDecl;
38 class ModuleMap;
39 class ObjCInterfaceDecl;
40 class ObjCIvarDecl;
41 class UsingDecl;
42 class VarDecl;
43 
44 namespace CodeGen {
45 class CodeGenModule;
46 class CodeGenFunction;
47 class CGBlockInfo;
48 
49 /// This class gathers all debug information during compilation and is
50 /// responsible for emitting to llvm globals or pass directly to the
51 /// backend.
52 class CGDebugInfo {
53   friend class ApplyDebugLocation;
54   friend class SaveAndRestoreLocation;
55   CodeGenModule &CGM;
56   const codegenoptions::DebugInfoKind DebugKind;
57   bool DebugTypeExtRefs;
58   llvm::DIBuilder DBuilder;
59   llvm::DICompileUnit *TheCU = nullptr;
60   ModuleMap *ClangModuleMap = nullptr;
61   ExternalASTSource::ASTSourceDescriptor PCHDescriptor;
62   SourceLocation CurLoc;
63   llvm::DIType *VTablePtrType = nullptr;
64   llvm::DIType *ClassTy = nullptr;
65   llvm::DICompositeType *ObjTy = nullptr;
66   llvm::DIType *SelTy = nullptr;
67   llvm::DIType *OCLImage1dDITy = nullptr;
68   llvm::DIType *OCLImage1dArrayDITy = nullptr;
69   llvm::DIType *OCLImage1dBufferDITy = nullptr;
70   llvm::DIType *OCLImage2dDITy = nullptr;
71   llvm::DIType *OCLImage2dArrayDITy = nullptr;
72   llvm::DIType *OCLImage2dDepthDITy = nullptr;
73   llvm::DIType *OCLImage2dArrayDepthDITy = nullptr;
74   llvm::DIType *OCLImage2dMSAADITy = nullptr;
75   llvm::DIType *OCLImage2dArrayMSAADITy = nullptr;
76   llvm::DIType *OCLImage2dMSAADepthDITy = nullptr;
77   llvm::DIType *OCLImage2dArrayMSAADepthDITy = nullptr;
78   llvm::DIType *OCLImage3dDITy = nullptr;
79   llvm::DIType *OCLEventDITy = nullptr;
80   llvm::DIType *OCLClkEventDITy = nullptr;
81   llvm::DIType *OCLQueueDITy = nullptr;
82   llvm::DIType *OCLNDRangeDITy = nullptr;
83   llvm::DIType *OCLReserveIDDITy = nullptr;
84 
85   /// Cache of previously constructed Types.
86   llvm::DenseMap<const void *, llvm::TrackingMDRef> TypeCache;
87 
88   llvm::SmallDenseMap<llvm::StringRef, llvm::StringRef> DebugPrefixMap;
89 
90   struct ObjCInterfaceCacheEntry {
91     const ObjCInterfaceType *Type;
92     llvm::DIType *Decl;
93     llvm::DIFile *Unit;
94     ObjCInterfaceCacheEntry(const ObjCInterfaceType *Type, llvm::DIType *Decl,
95                             llvm::DIFile *Unit)
96         : Type(Type), Decl(Decl), Unit(Unit) {}
97   };
98 
99   /// Cache of previously constructed interfaces which may change.
100   llvm::SmallVector<ObjCInterfaceCacheEntry, 32> ObjCInterfaceCache;
101 
102   /// Cache of references to clang modules and precompiled headers.
103   llvm::DenseMap<const Module *, llvm::TrackingMDRef> ModuleCache;
104 
105   /// List of interfaces we want to keep even if orphaned.
106   std::vector<void *> RetainedTypes;
107 
108   /// Cache of forward declared types to RAUW at the end of
109   /// compilation.
110   std::vector<std::pair<const TagType *, llvm::TrackingMDRef>> ReplaceMap;
111 
112   /// Cache of replaceable forward declarations (functions and
113   /// variables) to RAUW at the end of compilation.
114   std::vector<std::pair<const DeclaratorDecl *, llvm::TrackingMDRef>>
115       FwdDeclReplaceMap;
116 
117   /// Keep track of our current nested lexical block.
118   std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;
119   llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;
120   /// Keep track of LexicalBlockStack counter at the beginning of a
121   /// function. This is used to pop unbalanced regions at the end of a
122   /// function.
123   std::vector<unsigned> FnBeginRegionCount;
124 
125   /// This is a storage for names that are constructed on demand. For
126   /// example, C++ destructors, C++ operators etc..
127   llvm::BumpPtrAllocator DebugInfoNames;
128   StringRef CWDName;
129 
130   llvm::DenseMap<const char *, llvm::TrackingMDRef> DIFileCache;
131   llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPCache;
132   /// Cache declarations relevant to DW_TAG_imported_declarations (C++
133   /// using declarations) that aren't covered by other more specific caches.
134   llvm::DenseMap<const Decl *, llvm::TrackingMDRef> DeclCache;
135   llvm::DenseMap<const NamespaceDecl *, llvm::TrackingMDRef> NameSpaceCache;
136   llvm::DenseMap<const NamespaceAliasDecl *, llvm::TrackingMDRef>
137       NamespaceAliasCache;
138   llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIDerivedType>>
139       StaticDataMemberCache;
140 
141   /// Helper functions for getOrCreateType.
142   /// @{
143   /// Currently the checksum of an interface includes the number of
144   /// ivars and property accessors.
145   llvm::DIType *CreateType(const BuiltinType *Ty);
146   llvm::DIType *CreateType(const ComplexType *Ty);
147   llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg);
148   llvm::DIType *CreateType(const TypedefType *Ty, llvm::DIFile *Fg);
149   llvm::DIType *CreateType(const TemplateSpecializationType *Ty,
150                            llvm::DIFile *Fg);
151   llvm::DIType *CreateType(const ObjCObjectPointerType *Ty, llvm::DIFile *F);
152   llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F);
153   llvm::DIType *CreateType(const BlockPointerType *Ty, llvm::DIFile *F);
154   llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F);
155   /// Get structure or union type.
156   llvm::DIType *CreateType(const RecordType *Tyg);
157   llvm::DIType *CreateTypeDefinition(const RecordType *Ty);
158   llvm::DICompositeType *CreateLimitedType(const RecordType *Ty);
159   void CollectContainingType(const CXXRecordDecl *RD,
160                              llvm::DICompositeType *CT);
161   /// Get Objective-C interface type.
162   llvm::DIType *CreateType(const ObjCInterfaceType *Ty, llvm::DIFile *F);
163   llvm::DIType *CreateTypeDefinition(const ObjCInterfaceType *Ty,
164                                      llvm::DIFile *F);
165   /// Get Objective-C object type.
166   llvm::DIType *CreateType(const ObjCObjectType *Ty, llvm::DIFile *F);
167   llvm::DIType *CreateType(const VectorType *Ty, llvm::DIFile *F);
168   llvm::DIType *CreateType(const ArrayType *Ty, llvm::DIFile *F);
169   llvm::DIType *CreateType(const LValueReferenceType *Ty, llvm::DIFile *F);
170   llvm::DIType *CreateType(const RValueReferenceType *Ty, llvm::DIFile *Unit);
171   llvm::DIType *CreateType(const MemberPointerType *Ty, llvm::DIFile *F);
172   llvm::DIType *CreateType(const AtomicType *Ty, llvm::DIFile *F);
173   llvm::DIType *CreateType(const PipeType *Ty, llvm::DIFile *F);
174   /// Get enumeration type.
175   llvm::DIType *CreateEnumType(const EnumType *Ty);
176   llvm::DIType *CreateTypeDefinition(const EnumType *Ty);
177   /// Look up the completed type for a self pointer in the TypeCache and
178   /// create a copy of it with the ObjectPointer and Artificial flags
179   /// set. If the type is not cached, a new one is created. This should
180   /// never happen though, since creating a type for the implicit self
181   /// argument implies that we already parsed the interface definition
182   /// and the ivar declarations in the implementation.
183   llvm::DIType *CreateSelfType(const QualType &QualTy, llvm::DIType *Ty);
184   /// @}
185 
186   /// Get the type from the cache or return null type if it doesn't
187   /// exist.
188   llvm::DIType *getTypeOrNull(const QualType);
189   /// Return the debug type for a C++ method.
190   /// \arg CXXMethodDecl is of FunctionType. This function type is
191   /// not updated to include implicit \c this pointer. Use this routine
192   /// to get a method type which includes \c this pointer.
193   llvm::DISubroutineType *getOrCreateMethodType(const CXXMethodDecl *Method,
194                                                 llvm::DIFile *F);
195   llvm::DISubroutineType *
196   getOrCreateInstanceMethodType(QualType ThisPtr, const FunctionProtoType *Func,
197                                 llvm::DIFile *Unit);
198   llvm::DISubroutineType *
199   getOrCreateFunctionType(const Decl *D, QualType FnType, llvm::DIFile *F);
200   /// \return debug info descriptor for vtable.
201   llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F);
202   /// \return namespace descriptor for the given namespace decl.
203   llvm::DINamespace *getOrCreateNameSpace(const NamespaceDecl *N);
204   llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty,
205                                       QualType PointeeTy, llvm::DIFile *F);
206   llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache);
207 
208   /// A helper function to create a subprogram for a single member
209   /// function GlobalDecl.
210   llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method,
211                                               llvm::DIFile *F,
212                                               llvm::DIType *RecordTy);
213 
214   /// A helper function to collect debug info for C++ member
215   /// functions. This is used while creating debug info entry for a
216   /// Record.
217   void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F,
218                                  SmallVectorImpl<llvm::Metadata *> &E,
219                                  llvm::DIType *T);
220 
221   /// A helper function to collect debug info for C++ base
222   /// classes. This is used while creating debug info entry for a
223   /// Record.
224   void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F,
225                        SmallVectorImpl<llvm::Metadata *> &EltTys,
226                        llvm::DIType *RecordTy);
227 
228   /// A helper function to collect template parameters.
229   llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList,
230                                           ArrayRef<TemplateArgument> TAList,
231                                           llvm::DIFile *Unit);
232   /// A helper function to collect debug info for function template
233   /// parameters.
234   llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD,
235                                                   llvm::DIFile *Unit);
236 
237   /// A helper function to collect debug info for template
238   /// parameters.
239   llvm::DINodeArray
240   CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS,
241                            llvm::DIFile *F);
242 
243   llvm::DIType *createFieldType(StringRef name, QualType type,
244                                 uint64_t sizeInBitsOverride, SourceLocation loc,
245                                 AccessSpecifier AS, uint64_t offsetInBits,
246                                 llvm::DIFile *tunit, llvm::DIScope *scope,
247                                 const RecordDecl *RD = nullptr);
248 
249   /// Helpers for collecting fields of a record.
250   /// @{
251   void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl,
252                                  SmallVectorImpl<llvm::Metadata *> &E,
253                                  llvm::DIType *RecordTy);
254   llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var,
255                                                llvm::DIType *RecordTy,
256                                                const RecordDecl *RD);
257   void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits,
258                                 llvm::DIFile *F,
259                                 SmallVectorImpl<llvm::Metadata *> &E,
260                                 llvm::DIType *RecordTy, const RecordDecl *RD);
261   void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F,
262                            SmallVectorImpl<llvm::Metadata *> &E,
263                            llvm::DICompositeType *RecordTy);
264 
265   /// If the C++ class has vtable info then insert appropriate debug
266   /// info entry in EltTys vector.
267   void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F,
268                          SmallVectorImpl<llvm::Metadata *> &EltTys);
269   /// @}
270 
271   /// Create a new lexical block node and push it on the stack.
272   void CreateLexicalBlock(SourceLocation Loc);
273 
274 public:
275   CGDebugInfo(CodeGenModule &CGM);
276   ~CGDebugInfo();
277 
278   void finalize();
279 
280   /// Module debugging: Support for building PCMs.
281   /// @{
282   /// Set the main CU's DwoId field to \p Signature.
283   void setDwoId(uint64_t Signature);
284 
285   /// When generating debug information for a clang module or
286   /// precompiled header, this module map will be used to determine
287   /// the module of origin of each Decl.
288   void setModuleMap(ModuleMap &MMap) { ClangModuleMap = &MMap; }
289 
290   /// When generating debug information for a clang module or
291   /// precompiled header, this module map will be used to determine
292   /// the module of origin of each Decl.
293   void setPCHDescriptor(ExternalASTSource::ASTSourceDescriptor PCH) {
294     PCHDescriptor = PCH;
295   }
296   /// @}
297 
298   /// Update the current source location. If \arg loc is invalid it is
299   /// ignored.
300   void setLocation(SourceLocation Loc);
301 
302   /// Emit metadata to indicate a change in line/column information in
303   /// the source file. If the location is invalid, the previous
304   /// location will be reused.
305   void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
306 
307   /// Emit a call to llvm.dbg.function.start to indicate
308   /// start of a new function.
309   /// \param Loc       The location of the function header.
310   /// \param ScopeLoc  The location of the function body.
311   void EmitFunctionStart(GlobalDecl GD, SourceLocation Loc,
312                          SourceLocation ScopeLoc, QualType FnType,
313                          llvm::Function *Fn, CGBuilderTy &Builder);
314 
315   /// Emit debug info for a function declaration.
316   void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType);
317 
318   /// Constructs the debug code for exiting a function.
319   void EmitFunctionEnd(CGBuilderTy &Builder);
320 
321   /// Emit metadata to indicate the beginning of a new lexical block
322   /// and push the block onto the stack.
323   void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc);
324 
325   /// Emit metadata to indicate the end of a new lexical block and pop
326   /// the current block.
327   void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc);
328 
329   /// Emit call to \c llvm.dbg.declare for an automatic variable
330   /// declaration.
331   void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI,
332                                  CGBuilderTy &Builder);
333 
334   /// Emit call to \c llvm.dbg.declare for an imported variable
335   /// declaration in a block.
336   void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable,
337                                          llvm::Value *storage,
338                                          CGBuilderTy &Builder,
339                                          const CGBlockInfo &blockInfo,
340                                          llvm::Instruction *InsertPoint = nullptr);
341 
342   /// Emit call to \c llvm.dbg.declare for an argument variable
343   /// declaration.
344   void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
345                                 unsigned ArgNo, CGBuilderTy &Builder);
346 
347   /// Emit call to \c llvm.dbg.declare for the block-literal argument
348   /// to a block invocation function.
349   void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
350                                             llvm::Value *Arg, unsigned ArgNo,
351                                             llvm::Value *LocalAddr,
352                                             CGBuilderTy &Builder);
353 
354   /// Emit information about a global variable.
355   void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
356 
357   /// Emit global variable's debug info.
358   void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
359 
360   /// Emit C++ using directive.
361   void EmitUsingDirective(const UsingDirectiveDecl &UD);
362 
363   /// Emit the type explicitly casted to.
364   void EmitExplicitCastType(QualType Ty);
365 
366   /// Emit C++ using declaration.
367   void EmitUsingDecl(const UsingDecl &UD);
368 
369   /// Emit an @import declaration.
370   void EmitImportDecl(const ImportDecl &ID);
371 
372   /// Emit C++ namespace alias.
373   llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA);
374 
375   /// Emit record type's standalone debug info.
376   llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L);
377 
378   /// Emit an Objective-C interface type standalone debug info.
379   llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
380 
381   /// Emit standalone debug info for a type.
382   llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc);
383 
384   void completeType(const EnumDecl *ED);
385   void completeType(const RecordDecl *RD);
386   void completeRequiredType(const RecordDecl *RD);
387   void completeClassData(const RecordDecl *RD);
388 
389   void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD);
390 
391 private:
392   /// Emit call to llvm.dbg.declare for a variable declaration.
393   void EmitDeclare(const VarDecl *decl, llvm::Value *AI,
394                    llvm::Optional<unsigned> ArgNo, CGBuilderTy &Builder);
395 
396   /// Build up structure info for the byref.  See \a BuildByRefType.
397   llvm::DIType *EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
398                                              uint64_t *OffSet);
399 
400   /// Get context info for the DeclContext of \p Decl.
401   llvm::DIScope *getDeclContextDescriptor(const Decl *D);
402   /// Get context info for a given DeclContext \p Decl.
403   llvm::DIScope *getContextDescriptor(const Decl *Context,
404                                       llvm::DIScope *Default);
405 
406   llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl);
407 
408   /// Create a forward decl for a RecordType in a given context.
409   llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *,
410                                                   llvm::DIScope *);
411 
412   /// Return current directory name.
413   StringRef getCurrentDirname();
414 
415   /// Create new compile unit.
416   void CreateCompileUnit();
417 
418   /// Remap a given path with the current debug prefix map
419   std::string remapDIPath(StringRef) const;
420 
421   /// Get the file debug info descriptor for the input location.
422   llvm::DIFile *getOrCreateFile(SourceLocation Loc);
423 
424   /// Get the file info for main compile unit.
425   llvm::DIFile *getOrCreateMainFile();
426 
427   /// Get the type from the cache or create a new type if necessary.
428   llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg);
429 
430   /// Get a reference to a clang module.  If \p CreateSkeletonCU is true,
431   /// this also creates a split dwarf skeleton compile unit.
432   llvm::DIModule *
433   getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod,
434                        bool CreateSkeletonCU);
435 
436   /// DebugTypeExtRefs: If \p D originated in a clang module, return it.
437   llvm::DIModule *getParentModuleOrNull(const Decl *D);
438 
439   /// Get the type from the cache or create a new partial type if
440   /// necessary.
441   llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty,
442                                                 llvm::DIFile *F);
443 
444   /// Create type metadata for a source language type.
445   llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg);
446 
447   /// Create new member and increase Offset by FType's size.
448   llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType,
449                                  StringRef Name, uint64_t *Offset);
450 
451   /// Retrieve the DIDescriptor, if any, for the canonical form of this
452   /// declaration.
453   llvm::DINode *getDeclarationOrDefinition(const Decl *D);
454 
455   /// \return debug info descriptor to describe method
456   /// declaration for the given method definition.
457   llvm::DISubprogram *getFunctionDeclaration(const Decl *D);
458 
459   /// \return debug info descriptor to describe in-class static data
460   /// member declaration for the given out-of-class definition.  If D
461   /// is an out-of-class definition of a static data member of a
462   /// class, find its corresponding in-class declaration.
463   llvm::DIDerivedType *
464   getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D);
465 
466   /// Create a subprogram describing the forward declaration
467   /// represented in the given FunctionDecl.
468   llvm::DISubprogram *getFunctionForwardDeclaration(const FunctionDecl *FD);
469 
470   /// Create a global variable describing the forward decalration
471   /// represented in the given VarDecl.
472   llvm::DIGlobalVariable *
473   getGlobalVariableForwardDeclaration(const VarDecl *VD);
474 
475   /// \brief Return a global variable that represents one of the
476   /// collection of global variables created for an anonmyous union.
477   ///
478   /// Recursively collect all of the member fields of a global
479   /// anonymous decl and create static variables for them. The first
480   /// time this is called it needs to be on a union and then from
481   /// there we can have additional unnamed fields.
482   llvm::DIGlobalVariable *
483   CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit,
484                          unsigned LineNo, StringRef LinkageName,
485                          llvm::GlobalVariable *Var, llvm::DIScope *DContext);
486 
487   /// Get function name for the given FunctionDecl. If the name is
488   /// constructed on demand (e.g., C++ destructor) then the name is
489   /// stored on the side.
490   StringRef getFunctionName(const FunctionDecl *FD);
491 
492   /// Returns the unmangled name of an Objective-C method.
493   /// This is the display name for the debugging info.
494   StringRef getObjCMethodName(const ObjCMethodDecl *FD);
495 
496   /// Return selector name. This is used for debugging
497   /// info.
498   StringRef getSelectorName(Selector S);
499 
500   /// Get class name including template argument list.
501   StringRef getClassName(const RecordDecl *RD);
502 
503   /// Get the vtable name for the given class.
504   StringRef getVTableName(const CXXRecordDecl *Decl);
505 
506   /// Get line number for the location. If location is invalid
507   /// then use current location.
508   unsigned getLineNumber(SourceLocation Loc);
509 
510   /// Get column number for the location. If location is
511   /// invalid then use current location.
512   /// \param Force  Assume DebugColumnInfo option is true.
513   unsigned getColumnNumber(SourceLocation Loc, bool Force = false);
514 
515   /// Collect various properties of a FunctionDecl.
516   /// \param GD  A GlobalDecl whose getDecl() must return a FunctionDecl.
517   void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
518                                 StringRef &Name, StringRef &LinkageName,
519                                 llvm::DIScope *&FDContext,
520                                 llvm::DINodeArray &TParamsArray,
521                                 unsigned &Flags);
522 
523   /// Collect various properties of a VarDecl.
524   void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
525                            unsigned &LineNo, QualType &T, StringRef &Name,
526                            StringRef &LinkageName, llvm::DIScope *&VDContext);
527 
528   /// Allocate a copy of \p A using the DebugInfoNames allocator
529   /// and return a reference to it. If multiple arguments are given the strings
530   /// are concatenated.
531   StringRef internString(StringRef A, StringRef B = StringRef()) {
532     char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size());
533     if (!A.empty())
534       std::memcpy(Data, A.data(), A.size());
535     if (!B.empty())
536       std::memcpy(Data + A.size(), B.data(), B.size());
537     return StringRef(Data, A.size() + B.size());
538   }
539 };
540 
541 /// A scoped helper to set the current debug location to the specified
542 /// location or preferred location of the specified Expr.
543 class ApplyDebugLocation {
544 private:
545   void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false);
546   ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty,
547                      SourceLocation TemporaryLocation);
548 
549   llvm::DebugLoc OriginalLocation;
550   CodeGenFunction *CGF;
551 
552 public:
553   /// Set the location to the (valid) TemporaryLocation.
554   ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation);
555   ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E);
556   ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc);
557   ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) {
558     Other.CGF = nullptr;
559   }
560 
561   ~ApplyDebugLocation();
562 
563   /// \brief Apply TemporaryLocation if it is valid. Otherwise switch
564   /// to an artificial debug location that has a valid scope, but no
565   /// line information.
566   ///
567   /// Artificial locations are useful when emitting compiler-generated
568   /// helper functions that have no source location associated with
569   /// them. The DWARF specification allows the compiler to use the
570   /// special line number 0 to indicate code that can not be
571   /// attributed to any source location. Note that passing an empty
572   /// SourceLocation to CGDebugInfo::setLocation() will result in the
573   /// last valid location being reused.
574   static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) {
575     return ApplyDebugLocation(CGF, false, SourceLocation());
576   }
577   /// \brief Apply TemporaryLocation if it is valid. Otherwise switch
578   /// to an artificial debug location that has a valid scope, but no
579   /// line information.
580   static ApplyDebugLocation
581   CreateDefaultArtificial(CodeGenFunction &CGF,
582                           SourceLocation TemporaryLocation) {
583     return ApplyDebugLocation(CGF, false, TemporaryLocation);
584   }
585 
586   /// Set the IRBuilder to not attach debug locations.  Note that
587   /// passing an empty SourceLocation to \a CGDebugInfo::setLocation()
588   /// will result in the last valid location being reused.  Note that
589   /// all instructions that do not have a location at the beginning of
590   /// a function are counted towards to function prologue.
591   static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) {
592     return ApplyDebugLocation(CGF, true, SourceLocation());
593   }
594 
595 };
596 
597 } // namespace CodeGen
598 } // namespace clang
599 
600 #endif // LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
601