[ms-inline asm] MSVC parses multiple __asm statements on a single line as one
statement.  For example,

  if (x)
    __asm out dx, ax  __asm out dx, ax

results in a single inline asm statement (i.e., both "out dx, ax" statements are
predicated on if(x)).


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@161986 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp
index 0af1d08..a934847 100644
--- a/lib/Parse/ParseStmt.cpp
+++ b/lib/Parse/ParseStmt.cpp
@@ -1686,11 +1686,6 @@
     if (Tok.is(tok::eof))
       break;
 
-    // The asm keyword is a statement separator, so multiple asm statements
-    // are allowed on a single line.
-    if (!InAsmComment && Tok.is(tok::kw_asm))
-      break;
-
     if (!InAsmComment && Tok.is(tok::semi)) {
       // A semicolon in an asm is the start of a comment.
       InAsmComment = true;
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index 3d58a5d..2aabd9e 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -2777,12 +2777,21 @@
   unsigned NumAsmStrings = 0;
   for (unsigned i = 0, e = AsmToks.size(); i != e; ++i) {
 
+    // Determine if this should be considered a new asm.
+    bool isNewAsm = i == 0 || AsmToks[i].isAtStartOfLine() ||
+      AsmToks[i].is(tok::kw_asm);
+
     // Emit the previous asm string.
-    if (i && AsmToks[i].isAtStartOfLine())
+    if (i && isNewAsm) {
       AsmStrings[NumAsmStrings++] = Asm.c_str();
+      if (AsmToks[i].is(tok::kw_asm)) {
+        ++i; // Skip __asm
+        assert (i != e && "Expected another token.");
+      }
+    }
 
     // Start a new asm string with the opcode.
-    if (i == 0 || AsmToks[i].isAtStartOfLine()) {
+    if (isNewAsm) {
       Asm = AsmToks[i].getIdentifierInfo()->getName().str();
       continue;
     }
@@ -2831,17 +2840,29 @@
 
 // Build the unmodified MSAsmString.
 static std::string buildMSAsmString(Sema &SemaRef,
-                                    ArrayRef<Token> AsmToks) {
+                                    ArrayRef<Token> AsmToks) {                                    
   assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!");
   SmallString<512> Asm;
   SmallString<512> TokenBuf;
   TokenBuf.resize(512);
+
   for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) {
-    bool StringInvalid = false;
-    if (i && AsmToks[i].isAtStartOfLine())
-      Asm += '\n';
-    else if (i && AsmToks[i].hasLeadingSpace())
+    bool isNewAsm = i == 0 || AsmToks[i].isAtStartOfLine() ||
+      AsmToks[i].is(tok::kw_asm);
+
+    if (isNewAsm) {
+      if (i)
+        Asm += '\n';
+      if (AsmToks[i].is(tok::kw_asm)) {
+        i++; // Skip __asm
+        assert (i != e && "Expected another token");
+      }
+    }
+
+    if (i && AsmToks[i].hasLeadingSpace() && !isNewAsm)
       Asm += ' ';
+
+    bool StringInvalid = false;
     Asm += SemaRef.PP.getSpelling(AsmToks[i], TokenBuf, &StringInvalid);
     assert (!StringInvalid && "Expected valid string!");
   }
@@ -2876,7 +2897,7 @@
   // FIXME: Count this while parsing.
   unsigned NumAsmStrings = 0;
   for (unsigned i = 0, e = AsmToks.size(); i != e; ++i)
-    if (AsmToks[i].isAtStartOfLine())
+    if (i == 0 || AsmToks[i].isAtStartOfLine() || AsmToks[i].is(tok::kw_asm))
       ++NumAsmStrings;
 
   PatchedAsmStrings.resize(NumAsmStrings ? NumAsmStrings : 1);
diff --git a/test/CodeGen/ms-inline-asm.c b/test/CodeGen/ms-inline-asm.c
index fe0d3a5..f54c226 100644
--- a/test/CodeGen/ms-inline-asm.c
+++ b/test/CodeGen/ms-inline-asm.c
@@ -20,9 +20,7 @@
 
 void t3() {
 // CHECK: @t3
-// CHECK: call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
-// CHECK: call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
-// CHECK: call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
+// CHECK: call void asm sideeffect "nop\0Anop\0Anop", "~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
 // CHECK: ret void
   __asm nop __asm nop __asm nop
 }
@@ -38,8 +36,7 @@
 
 void t5(void) {
 // CHECK: @t5
-// CHECK: call void asm sideeffect "mov ebx, eax", "~{ebx},~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
-// CHECK: call void asm sideeffect "mov ecx, ebx", "~{ecx},~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
+// CHECK: call void asm sideeffect "mov ebx, eax\0Amov ecx, ebx", "~{ebx},~{ecx},~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
 // CHECK: ret void
   __asm mov ebx, eax __asm mov ecx, ebx
 }
diff --git a/test/Parser/ms-inline-asm.c b/test/Parser/ms-inline-asm.c
index 6820f3e..5326ce4 100644
--- a/test/Parser/ms-inline-asm.c
+++ b/test/Parser/ms-inline-asm.c
@@ -28,10 +28,10 @@
   }
 }
 void t8() {
-  __asm nop __asm nop __asm nop // expected-warning {{MS-style inline assembly is not supported}} expected-warning {{MS-style inline assembly is not supported}} expected-warning {{MS-style inline assembly is not supported}}
+  __asm nop __asm nop __asm nop // expected-warning {{MS-style inline assembly is not supported}}
 }
 void t9() {
-  __asm nop __asm nop ; __asm nop // expected-warning {{MS-style inline assembly is not supported}} expected-warning {{MS-style inline assembly is not supported}}
+  __asm nop __asm nop ; __asm nop // expected-warning {{MS-style inline assembly is not supported}}
 }
 int t_fail() { // expected-note {{to match this}}
   __asm