Handle corner case where clang-cc is invoked directly to compile preprocessed source file without -main-file-name. In this case, CDDebugInfo is not able identify correct main source file becase SM.isFromMainFile() returns true for locations from header files as well as locations from main source file.
This patch takes conservative approach by not emitting more then one compile unit with isMain bit set.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69902 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp
index f2ca2e7..a652ede 100644
--- a/lib/CodeGen/CGDebugInfo.cpp
+++ b/lib/CodeGen/CGDebugInfo.cpp
@@ -34,7 +34,7 @@
 using namespace clang::CodeGen;
 
 CGDebugInfo::CGDebugInfo(CodeGenModule *m)
-  : M(m), DebugFactory(M->getModule()) {
+  : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()) {
 }
 
 CGDebugInfo::~CGDebugInfo() {
@@ -71,16 +71,22 @@
     AbsFileName = tmp;
   }
 
-  // See if thie compile unit is represnting main source file.
+  // See if thie compile unit is representing main source file. Each source
+  // file has corresponding compile unit. There is only one main source
+  // file at a time.
   bool isMain = false;
   const LangOptions &LO = M->getLangOptions();
   const char *MainFileName = LO.getMainFileName();
-  if (MainFileName) {
-    if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
-      isMain = true;
-  } else {
-    if (Loc.isValid() && SM.isFromMainFile(Loc))
-      isMain = true;
+  if (isMainCompileUnitCreated == false) {
+    if (MainFileName) {
+      if (!strcmp(AbsFileName.getLast().c_str(), MainFileName))
+        isMain = true;
+    } else {
+      if (Loc.isValid() && SM.isFromMainFile(Loc))
+        isMain = true;
+    }
+    if (isMain)
+      isMainCompileUnitCreated = true;
   }
 
   unsigned LangTag;
diff --git a/lib/CodeGen/CGDebugInfo.h b/lib/CodeGen/CGDebugInfo.h
index 4a59eca..1581637 100644
--- a/lib/CodeGen/CGDebugInfo.h
+++ b/lib/CodeGen/CGDebugInfo.h
@@ -34,6 +34,7 @@
 /// the backend.
 class CGDebugInfo {
   CodeGenModule *M;
+  bool isMainCompileUnitCreated;
   llvm::DIFactory DebugFactory;
   
   SourceLocation CurLoc, PrevLoc;