Debug info: Ensure that the last stop point in a function is still within
the lexical block formed by the compound statement that is the function
body.

rdar://problem/15010825

llvm-svn: 198461
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index fec1c33..c12e294 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -2537,7 +2537,8 @@
 /// information in the source file. If the location is invalid, the
 /// previous location will be reused.
 void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
-                               bool ForceColumnInfo) {
+                               bool ForceColumnInfo,
+                               llvm::MDNode *ForceScope) {
   // Update our current location
   setLocation(Loc);
 
@@ -2556,7 +2557,7 @@
   // Update last state.
   PrevLoc = CurLoc;
 
-  llvm::MDNode *Scope = LexicalBlockStack.back();
+  llvm::MDNode *Scope = ForceScope ? ForceScope : &*LexicalBlockStack.back();
   Builder.SetCurrentDebugLocation(llvm::DebugLoc::get
                                   (getLineNumber(CurLoc),
                                    getColumnNumber(CurLoc, ForceColumnInfo),
diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index 0ca274f..ac31bdf 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -211,11 +211,17 @@
   /// getLocation - Return the current source location.
   SourceLocation getLocation() const { return CurLoc; }
 
+  /// getScope() - Return the current scope.
+  llvm::MDNode *getScope() const { return LexicalBlockStack.back(); }
+
   /// EmitLocation - Emit metadata to indicate a change in line/column
   /// information in the source file.
   /// \param ForceColumnInfo  Assume DebugColumnInfo option is true.
+  /// \param ForceScope       Force the location to be in a specific lexical
+  ///                         scope rather than the top of LexicalBlockStack.
   void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc,
-                    bool ForceColumnInfo = false);
+                    bool ForceColumnInfo = false,
+                    llvm::MDNode *ForceScope = 0);
 
   /// EmitFunctionStart - Emit a call to llvm.dbg.function.start to indicate
   /// start of a new function.
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 779054b..6224839 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -37,7 +37,7 @@
     Loc = S->getLocStart();
     DI->EmitLocation(Builder, Loc);
 
-    LastStopPoint = Loc;
+    LastStopPoint = std::make_pair(Loc, DI->getScope());
   }
 }
 
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index cebd6a7..2d8c68e 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -207,9 +207,10 @@
   // all will be fine.
   if (CGDebugInfo *DI = getDebugInfo()) {
     if (OnlySimpleReturnStmts)
-      DI->EmitLocation(Builder, LastStopPoint);
+      DI->EmitLocation(Builder, LastStopPoint.first,
+                       false, LastStopPoint.second);
     else
-      DI->EmitLocation(Builder, EndLoc);
+      DI->EmitLocation(Builder, EndLoc, false, LastStopPoint.second);
   }
 
   // Pop any cleanups that might have been associated with the
@@ -226,7 +227,7 @@
 
     if (CGDebugInfo *DI = getDebugInfo())
       if (OnlySimpleReturnStmts)
-        DI->EmitLocation(Builder, EndLoc);
+        DI->EmitLocation(Builder, EndLoc, false, LastStopPoint.second);
   }
 
   // Emit function epilog (to return).
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 6c00b2a..1dd5dab 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -859,7 +859,7 @@
   unsigned NumSimpleReturnExprs;
 
   /// The last regular (non-return) debug location (breakpoint) in the function.
-  SourceLocation LastStopPoint;
+  std::pair<SourceLocation, llvm::MDNode*> LastStopPoint;
 
 public:
   /// A scope within which we are constructing the fields of an object which