In Dwarf 3 (and Dwarf 2) attributes whose value are offsets into a
section use the form DW_FORM_data4 whilst in Dwarf 4 and later they
use the form DW_FORM_sec_offset.

This patch updates the places where such attributes are generated to
use the appropriate form depending on the Dwarf version. The DIE entries
affected have the following tags:
DW_AT_stmt_list, DW_AT_ranges, DW_AT_location, DW_AT_GNU_pubnames,
DW_AT_GNU_pubtypes, DW_AT_GNU_addr_base, DW_AT_GNU_ranges_base

It also adds a hidden command line option "--dwarf-version=<uint>"
to llc which allows the version of Dwarf to be generated to override
what is specified in the metadata; this makes it possible to update
existing tests to check the debugging information generated for both
Dwarf 4 (the default) and Dwarf 3 using the same metadata.

Patch (slightly modified) by Keith Walker!

llvm-svn: 195391
diff --git a/llvm/test/DebugInfo/X86/gnu-public-names.ll b/llvm/test/DebugInfo/X86/gnu-public-names.ll
index a9fc0ed..e2e2b3a 100644
--- a/llvm/test/DebugInfo/X86/gnu-public-names.ll
+++ b/llvm/test/DebugInfo/X86/gnu-public-names.ll
@@ -1,5 +1,6 @@
 ; RUN: llc -mtriple=x86_64-pc-linux-gnu -generate-gnu-dwarf-pub-sections < %s | FileCheck -check-prefix=ASM %s
 ; RUN: llc -mtriple=x86_64-pc-linux-gnu -generate-gnu-dwarf-pub-sections -filetype=obj < %s | llvm-dwarfdump - | FileCheck %s
+; RUN: llc -mtriple=x86_64-pc-linux-gnu -generate-gnu-dwarf-pub-sections -filetype=obj -dwarf-version=3 < %s | llvm-dwarfdump - | FileCheck %s -check-prefix=DWARF3
 ; ModuleID = 'dwarf-public-names.cpp'
 ;
 ; Generated from:
@@ -123,6 +124,85 @@
 ; CHECK-DAG:  [[D]] EXTERNAL TYPE     "ns::D"
 ; CHECK-DAG:  [[INT]] STATIC   TYPE     "int"
 
+; DWARF3: .debug_info contents:
+; DWARF3: DW_AT_GNU_pubnames [DW_FORM_data4]   (0x00000000)
+; DWARF3: DW_AT_GNU_pubtypes [DW_FORM_data4]   (0x00000000)
+
+; DWARF3: [[C:[0-9a-f]+]]: DW_TAG_structure_type
+; DWARF3-NEXT: DW_AT_name {{.*}} "C"
+
+; DWARF3: [[STATIC_MEM_DECL:[0-9a-f]+]]: DW_TAG_member
+; DWARF3-NEXT: DW_AT_name {{.*}} "static_member_variable"
+
+; DWARF3: [[MEM_FUNC_DECL:[0-9a-f]+]]: DW_TAG_subprogram
+; DWARF3-NEXT: DW_AT_MIPS_linkage_name
+; DWARF3-NEXT: DW_AT_name {{.*}} "member_function"
+
+; DWARF3: [[STATIC_MEM_FUNC_DECL:[0-9a-f]+]]: DW_TAG_subprogram
+; DWARF3-NEXT: DW_AT_MIPS_linkage_name
+; DWARF3-NEXT: DW_AT_name {{.*}} "static_member_function"
+
+; DWARF3: [[INT:[0-9a-f]+]]: DW_TAG_base_type
+; DWARF3-NEXT: DW_AT_name {{.*}} "int"
+
+; DWARF3: [[STATIC_MEM_VAR:[0-9a-f]+]]: DW_TAG_variable
+; DWARF3-NEXT: DW_AT_specification {{.*}}[[STATIC_MEM_DECL]]
+
+; DWARF3: [[GLOB_VAR:[0-9a-f]+]]: DW_TAG_variable
+; DWARF3-NEXT: DW_AT_name {{.*}} "global_variable"
+
+; DWARF3: [[NS:[0-9a-f]+]]: DW_TAG_namespace
+; DWARF3-NEXT: DW_AT_name {{.*}} "ns"
+
+; DWARF3: [[GLOB_NS_VAR_DECL:[0-9a-f]+]]: DW_TAG_variable
+; DWARF3-NEXT: DW_AT_name {{.*}} "global_namespace_variable"
+
+; DWARF3: [[D_VAR_DECL:[0-9a-f]+]]: DW_TAG_variable
+; DWARF3-NEXT: DW_AT_name {{.*}} "d"
+
+; DWARF3: [[D:[0-9a-f]+]]: DW_TAG_structure_type
+; DWARF3-NEXT: DW_AT_name {{.*}} "D"
+
+; DWARF3: [[GLOB_NS_FUNC:[0-9a-f]+]]: DW_TAG_subprogram
+; DWARF3-NEXT: DW_AT_MIPS_linkage_name
+; DWARF3-NEXT: DW_AT_name {{.*}} "global_namespace_function"
+
+; DWARF3: [[GLOB_NS_VAR:[0-9a-f]+]]: DW_TAG_variable
+; DWARF3-NEXT: DW_AT_specification {{.*}}[[GLOB_NS_VAR_DECL]]
+
+; DWARF3: [[D_VAR:[0-9a-f]+]]: DW_TAG_variable
+; DWARF3-NEXT: DW_AT_specification {{.*}}[[D_VAR_DECL]]
+
+; DWARF3: [[MEM_FUNC:[0-9a-f]+]]: DW_TAG_subprogram
+; DWARF3-NEXT: DW_AT_specification {{.*}}[[MEM_FUNC_DECL]]
+
+; DWARF3: [[STATIC_MEM_FUNC:[0-9a-f]+]]: DW_TAG_subprogram
+; DWARF3-NEXT: DW_AT_specification {{.*}}[[STATIC_MEM_FUNC_DECL]]
+
+; DWARF3: [[GLOBAL_FUNC:[0-9a-f]+]]: DW_TAG_subprogram
+; DWARF3-NEXT: DW_AT_MIPS_linkage_name
+; DWARF3-NEXT: DW_AT_name {{.*}} "global_function"
+
+; DWARF3-LABEL: .debug_gnu_pubnames contents:
+; DWARF3-NEXT: length = 0x000000e7 version = 0x0002 unit_offset = 0x00000000 unit_size = 0x0000018b
+; DWARF3-NEXT: Offset     Linkage  Kind     Name
+; DWARF3-DAG:  [[GLOBAL_FUNC]] EXTERNAL FUNCTION "global_function"
+; DWARF3-DAG:  [[NS]] EXTERNAL TYPE     "ns"
+; DWARF3-DAG:  [[MEM_FUNC]] EXTERNAL FUNCTION "C::member_function"
+; DWARF3-DAG:  [[GLOB_VAR]] EXTERNAL VARIABLE "global_variable"
+; DWARF3-DAG:  [[GLOB_NS_VAR]] EXTERNAL VARIABLE "ns::global_namespace_variable"
+; DWARF3-DAG:  [[GLOB_NS_FUNC]] EXTERNAL FUNCTION "ns::global_namespace_function"
+; DWARF3-DAG:  [[D_VAR]] EXTERNAL VARIABLE "ns::d"
+; DWARF3-DAG:  [[STATIC_MEM_VAR]] EXTERNAL VARIABLE "C::static_member_variable"
+; DWARF3-DAG:  [[STATIC_MEM_FUNC]] EXTERNAL FUNCTION "C::static_member_function"
+
+
+; DWARF3-LABEL: debug_gnu_pubtypes contents:
+; DWARF3: Offset     Linkage  Kind     Name
+; DWARF3-DAG:  [[C]] EXTERNAL TYPE     "C"
+; DWARF3-DAG:  [[D]] EXTERNAL TYPE     "ns::D"
+; DWARF3-DAG:  [[INT]] STATIC   TYPE     "int"
+
 %struct.C = type { i8 }
 %"struct.ns::D" = type { i32 }