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