[OPENMP] Delay emission of the asm target-specific error messages.

Summary:
Added the ability to emit target-specific builtin assembler error
messages only in case if the function is really is going to be emitted
for the device.

Reviewers: rjmccall

Subscribers: guansong, jdoerfert, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D58243

llvm-svn: 354486
diff --git a/clang/lib/Sema/SemaStmtAsm.cpp b/clang/lib/Sema/SemaStmtAsm.cpp
index d9b39e8..0848ed4 100644
--- a/clang/lib/Sema/SemaStmtAsm.cpp
+++ b/clang/lib/Sema/SemaStmtAsm.cpp
@@ -272,9 +272,9 @@
 
     TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
     if (!Context.getTargetInfo().validateOutputConstraint(Info))
-      return StmtError(
-          Diag(Literal->getBeginLoc(), diag::err_asm_invalid_output_constraint)
-          << Info.getConstraintStr());
+      return StmtResult(targetDiag(Literal->getBeginLoc(),
+                                   diag::err_asm_invalid_output_constraint)
+                        << Info.getConstraintStr());
 
     ExprResult ER = CheckPlaceholderExpr(Exprs[i]);
     if (ER.isInvalid())
@@ -327,11 +327,10 @@
     }
 
     unsigned Size = Context.getTypeSize(OutputExpr->getType());
-    if (!Context.getTargetInfo().validateOutputSize(Literal->getString(),
-                                                    Size))
-      return StmtError(
-          Diag(OutputExpr->getBeginLoc(), diag::err_asm_invalid_output_size)
-          << Info.getConstraintStr());
+    if (!Context.getTargetInfo().validateOutputSize(Literal->getString(), Size))
+      return StmtResult(targetDiag(OutputExpr->getBeginLoc(),
+                                   diag::err_asm_invalid_output_size)
+                        << Info.getConstraintStr());
   }
 
   SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
@@ -347,9 +346,9 @@
     TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
     if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos,
                                                          Info)) {
-      return StmtError(
-          Diag(Literal->getBeginLoc(), diag::err_asm_invalid_input_constraint)
-          << Info.getConstraintStr());
+      return StmtResult(targetDiag(Literal->getBeginLoc(),
+                                   diag::err_asm_invalid_input_constraint)
+                        << Info.getConstraintStr());
     }
 
     ExprResult ER = CheckPlaceholderExpr(Exprs[i]);
@@ -421,8 +420,8 @@
     unsigned Size = Context.getTypeSize(Ty);
     if (!Context.getTargetInfo().validateInputSize(Literal->getString(),
                                                    Size))
-      return StmtError(
-          Diag(InputExpr->getBeginLoc(), diag::err_asm_invalid_input_size)
+      return StmtResult(
+          targetDiag(InputExpr->getBeginLoc(), diag::err_asm_invalid_input_size)
           << Info.getConstraintStr());
   }
 
@@ -434,9 +433,9 @@
     StringRef Clobber = Literal->getString();
 
     if (!Context.getTargetInfo().isValidClobber(Clobber))
-      return StmtError(
-          Diag(Literal->getBeginLoc(), diag::err_asm_unknown_register_name)
-          << Clobber);
+      return StmtResult(targetDiag(Literal->getBeginLoc(),
+                                   diag::err_asm_unknown_register_name)
+                        << Clobber);
   }
 
   GCCAsmStmt *NS =
@@ -447,11 +446,10 @@
   // have.
   SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces;
   unsigned DiagOffs;
-  if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
-    Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
-           << AsmString->getSourceRange();
-    return StmtError();
-  }
+  if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs))
+    return StmtResult(
+        targetDiag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
+        << AsmString->getSourceRange());
 
   // Validate constraints and modifiers.
   for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
@@ -488,16 +486,15 @@
     if (!Context.getTargetInfo().validateConstraintModifier(
             Literal->getString(), Piece.getModifier(), Size,
             SuggestedModifier)) {
-      Diag(Exprs[ConstraintIdx]->getBeginLoc(),
-           diag::warn_asm_mismatched_size_modifier);
+      targetDiag(Exprs[ConstraintIdx]->getBeginLoc(),
+                 diag::warn_asm_mismatched_size_modifier);
 
       if (!SuggestedModifier.empty()) {
-        auto B = Diag(Piece.getRange().getBegin(),
-                      diag::note_asm_missing_constraint_modifier)
+        auto B = targetDiag(Piece.getRange().getBegin(),
+                            diag::note_asm_missing_constraint_modifier)
                  << SuggestedModifier;
         SuggestedModifier = "%" + SuggestedModifier + Piece.getString();
-        B.AddFixItHint(FixItHint::CreateReplacement(Piece.getRange(),
-                                                    SuggestedModifier));
+        B << FixItHint::CreateReplacement(Piece.getRange(), SuggestedModifier);
       }
     }
   }
@@ -511,9 +508,10 @@
     if (NumAlternatives == ~0U)
       NumAlternatives = AltCount;
     else if (NumAlternatives != AltCount)
-      return StmtError(Diag(NS->getOutputExpr(i)->getBeginLoc(),
-                            diag::err_asm_unexpected_constraint_alternatives)
-                       << NumAlternatives << AltCount);
+      return StmtResult(
+          targetDiag(NS->getOutputExpr(i)->getBeginLoc(),
+                     diag::err_asm_unexpected_constraint_alternatives)
+          << NumAlternatives << AltCount);
   }
   SmallVector<size_t, 4> InputMatchedToOutput(OutputConstraintInfos.size(),
                                               ~0U);
@@ -524,9 +522,10 @@
     if (NumAlternatives == ~0U)
       NumAlternatives = AltCount;
     else if (NumAlternatives != AltCount)
-      return StmtError(Diag(NS->getInputExpr(i)->getBeginLoc(),
-                            diag::err_asm_unexpected_constraint_alternatives)
-                       << NumAlternatives << AltCount);
+      return StmtResult(
+          targetDiag(NS->getInputExpr(i)->getBeginLoc(),
+                     diag::err_asm_unexpected_constraint_alternatives)
+          << NumAlternatives << AltCount);
 
     // If this is a tied constraint, verify that the output and input have
     // either exactly the same type, or that they are int/ptr operands with the
@@ -541,13 +540,14 @@
     // Make sure no more than one input constraint matches each output.
     assert(TiedTo < InputMatchedToOutput.size() && "TiedTo value out of range");
     if (InputMatchedToOutput[TiedTo] != ~0U) {
-      Diag(NS->getInputExpr(i)->getBeginLoc(),
-           diag::err_asm_input_duplicate_match)
+      targetDiag(NS->getInputExpr(i)->getBeginLoc(),
+                 diag::err_asm_input_duplicate_match)
           << TiedTo;
-      Diag(NS->getInputExpr(InputMatchedToOutput[TiedTo])->getBeginLoc(),
-           diag::note_asm_input_duplicate_first)
-          << TiedTo;
-      return StmtError();
+      return StmtResult(
+          targetDiag(
+              NS->getInputExpr(InputMatchedToOutput[TiedTo])->getBeginLoc(),
+              diag::note_asm_input_duplicate_first)
+          << TiedTo);
     }
     InputMatchedToOutput[TiedTo] = i;
 
@@ -632,10 +632,10 @@
       continue;
     }
 
-    Diag(InputExpr->getBeginLoc(), diag::err_asm_tying_incompatible_types)
-        << InTy << OutTy << OutputExpr->getSourceRange()
-        << InputExpr->getSourceRange();
-    return StmtError();
+    return StmtResult(targetDiag(InputExpr->getBeginLoc(),
+                                 diag::err_asm_tying_incompatible_types)
+                      << InTy << OutTy << OutputExpr->getSourceRange()
+                      << InputExpr->getSourceRange());
   }
 
   // Check for conflicts between clobber list and input or output lists
@@ -643,7 +643,8 @@
       getClobberConflictLocation(Exprs, Constraints, Clobbers, NumClobbers,
                                  Context.getTargetInfo(), Context);
   if (ConstraintLoc.isValid())
-    return Diag(ConstraintLoc, diag::error_inoutput_conflict_with_clobber);
+    return StmtResult(
+        targetDiag(ConstraintLoc, diag::error_inoutput_conflict_with_clobber));
 
   return NS;
 }