Deduce whether the module contains debug info.

Add functionality to the MetadataExtractor to deduce whether the
module contains debug info. This method will be used to deduce
whether we should link with a special runtime library with debug
information.

Change-Id: I8ebe8c465c785adfb2b24864714e654f49c12c6d
diff --git a/bcinfo/MetadataExtractor.cpp b/bcinfo/MetadataExtractor.cpp
index f190c48..96f3692 100644
--- a/bcinfo/MetadataExtractor.cpp
+++ b/bcinfo/MetadataExtractor.cpp
@@ -171,6 +171,11 @@
 // be synced with libbcc/lib/Core/Source.cpp)
 static const llvm::StringRef ChecksumMetadataName = "#rs_build_checksum";
 
+// Name of metadata node which contains a list of compile units that have debug
+// metadata. If this is null then there is no debug metadata in the compile
+// unit.
+static const llvm::StringRef DebugInfoMetadataName = "llvm.dbg.cu";
+
 MetadataExtractor::MetadataExtractor(const char *bitcode, size_t bitcodeSize)
     : mModule(nullptr), mBitcode(bitcode), mBitcodeSize(bitcodeSize),
       mExportVarCount(0), mExportFuncCount(0), mExportForEachSignatureCount(0),
@@ -182,7 +187,7 @@
       mPragmaCount(0), mPragmaKeyList(nullptr), mPragmaValueList(nullptr),
       mObjectSlotCount(0), mObjectSlotList(nullptr),
       mRSFloatPrecision(RS_FP_Full), mIsThreadable(true),
-      mBuildChecksum(nullptr) {
+      mBuildChecksum(nullptr), mHasDebugInfo(false) {
   BitcodeWrapper wrapper(bitcode, bitcodeSize);
   mTargetAPI = wrapper.getTargetAPI();
   mCompilerVersion = wrapper.getCompilerVersion();
@@ -628,6 +633,8 @@
       mModule->getNamedMetadata(ThreadableMetadataName);
   const llvm::NamedMDNode *ChecksumMetadata =
       mModule->getNamedMetadata(ChecksumMetadataName);
+  const llvm::NamedMDNode *DebugInfoMetadata =
+      mModule->getNamedMetadata(DebugInfoMetadataName);
 
   if (!populateNameMetadata(ExportVarMetadata, mExportVarNameList,
                             mExportVarCount)) {
@@ -668,6 +675,8 @@
   readThreadableFlag(ThreadableMetadata);
   readBuildChecksumMetadata(ChecksumMetadata);
 
+  mHasDebugInfo = DebugInfoMetadata != nullptr;
+
   return true;
 }
 
diff --git a/include/bcinfo/MetadataExtractor.h b/include/bcinfo/MetadataExtractor.h
index 738c1c2..fcaaee8 100644
--- a/include/bcinfo/MetadataExtractor.h
+++ b/include/bcinfo/MetadataExtractor.h
@@ -116,6 +116,8 @@
 
   const char *mBuildChecksum;
 
+  bool mHasDebugInfo;
+
   // Helper functions for extraction
   bool populateForEachMetadata(const llvm::NamedMDNode *Names,
                                const llvm::NamedMDNode *Signatures);
@@ -399,6 +401,13 @@
   const char *getBuildChecksum() const {
     return mBuildChecksum;
   }
+
+  /**
+   * \return whether the module contains debug metadata
+   */
+  bool hasDebugInfo() const {
+    return mHasDebugInfo;
+  }
 };
 
 }  // namespace bcinfo