[PATCH] D102244: [llvm][AsmPrinter] Restore source location to register clobber warning

David Spickett via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 12 07:41:31 PDT 2021


DavidSpickett updated this revision to Diff 344821.
DavidSpickett added a comment.
Herald added a subscriber: dexonsmith.

- Report via LLVMContext


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102244/new/

https://reviews.llvm.org/D102244

Files:
  clang/test/Misc/inline-asm-clobber-warning.c
  llvm/include/llvm/IR/LLVMContext.h
  llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
  llvm/lib/IR/LLVMContext.cpp


Index: llvm/lib/IR/LLVMContext.cpp
===================================================================
--- llvm/lib/IR/LLVMContext.cpp
+++ llvm/lib/IR/LLVMContext.cpp
@@ -252,6 +252,14 @@
   diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
 }
 
+void LLVMContext::emitWarning(unsigned LocCookie, const Twine &WarningStr) {
+  diagnose(DiagnosticInfoInlineAsm(LocCookie, WarningStr, DS_Warning));
+}
+
+void LLVMContext::emitNote(unsigned LocCookie, const Twine &NoteStr) {
+  diagnose(DiagnosticInfoInlineAsm(LocCookie, NoteStr, DS_Note));
+}
+
 //===----------------------------------------------------------------------===//
 // Metadata Kind Uniquing
 //===----------------------------------------------------------------------===//
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
@@ -527,11 +527,6 @@
   }
 
   if (!RestrRegs.empty()) {
-    unsigned BufNum = addInlineAsmDiagBuffer(OS.str(), LocMD);
-    auto &SrcMgr = *MMI->getContext().getInlineSourceManager();
-    SMLoc Loc = SMLoc::getFromPointer(
-        SrcMgr.getMemoryBuffer(BufNum)->getBuffer().begin());
-
     std::string Msg = "inline asm clobber list contains reserved registers: ";
     ListSeparator LS;
     for (const Register &RR : RestrRegs) {
@@ -542,8 +537,8 @@
         "Reserved registers on the clobber list may not be "
         "preserved across the asm statement, and clobbering them may "
         "lead to undefined behaviour.";
-    SrcMgr.PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
-    SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, Note);
+    MMI->getModule()->getContext().emitWarning(LocCookie, Msg.c_str());
+    MMI->getModule()->getContext().emitNote(LocCookie, Note);
   }
 
   emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
Index: llvm/include/llvm/IR/LLVMContext.h
===================================================================
--- llvm/include/llvm/IR/LLVMContext.h
+++ llvm/include/llvm/IR/LLVMContext.h
@@ -294,6 +294,16 @@
   void emitError(const Instruction *I, const Twine &ErrorStr);
   void emitError(const Twine &ErrorStr);
 
+  /// emitWarning - Emit a warning message to the currently installed warning
+  /// handler with location information. Message will be implicitly prefixed
+  /// with "warning: " and should not end with a ".".
+  void emitWarning(unsigned LocCookie, const Twine &WarningStr);
+
+  /// emitNote - Emit a note to the currently installed note handler with
+  /// location information. Same rules as emitWarning except the prefix will
+  /// be "note: ".
+  void emitNote(unsigned LocCookie, const Twine &NoteStr);
+
   /// Access the object which can disable optional passes and individual
   /// optimizations at compile time.
   OptPassGate &getOptPassGate() const;
Index: clang/test/Misc/inline-asm-clobber-warning.c
===================================================================
--- /dev/null
+++ clang/test/Misc/inline-asm-clobber-warning.c
@@ -0,0 +1,18 @@
+/// This test checks that the warning includes the location in the C source
+/// file that contains the inline asm. Instead of saying <inline asm> for both.
+/// Although this warning is emitted in llvm it cannot be tested from IR as
+/// it does not have that location information at that stage.
+
+// RUN: %clang -target arm-arm-none-eabi -march=armv7-m -c %s -o /dev/null \
+// RUN:   2>&1 | FileCheck %s
+
+// REQUIRES: arm-registered-target
+
+void bar(void) {
+    __asm__ __volatile__ ( "nop" : : : "sp");
+}
+
+// CHECK:      inline-asm-clobber-warning.c:12:28: warning: inline asm clobber list contains reserved registers: SP [-Winline-asm]
+// CHECK-NEXT:     __asm__ __volatile__ ( "nop" : : : "sp");
+// CHECK-NEXT:                            ^
+// CHECK-NEXT: inline-asm-clobber-warning.c:12:28: note: Reserved registers on the clobber list may not be preserved across the asm statement, and clobbering them may lead to undefined behaviour.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102244.344821.patch
Type: text/x-patch
Size: 4089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210512/46e5cb14/attachment.bin>


More information about the cfe-commits mailing list