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