[PATCH] D67449: [llvm-ar] Include a line number when failing to parse an MRI script

Owen Reynolds via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 11 08:12:16 PDT 2019


gbreynoo created this revision.
gbreynoo added reviewers: rupprecht, MaskRay.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Errors that occur when reading an MRI script would be clearer if they included a corresponding line number.


Repository:
  rL LLVM

https://reviews.llvm.org/D67449

Files:
  llvm/test/tools/llvm-ar/mri-errors.test
  llvm/tools/llvm-ar/llvm-ar.cpp


Index: llvm/tools/llvm-ar/llvm-ar.cpp
===================================================================
--- llvm/tools/llvm-ar/llvm-ar.cpp
+++ llvm/tools/llvm-ar/llvm-ar.cpp
@@ -116,10 +116,19 @@
     outs() << ArHelp;
 }
 
+static unsigned MRILineNumber;
+static bool ParsingMRIScript;
+
 // Show the error message and exit.
 LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
-  WithColor::error(errs(), ToolName) << Error << ".\n";
-  printHelpMessage();
+  if (ParsingMRIScript) {
+    WithColor::error(errs(), ToolName)
+        << "script line " << MRILineNumber << ": " << Error << ".\n";
+  } else {
+    WithColor::error(errs(), ToolName) << Error << ".\n";
+    printHelpMessage();
+  }
+
   exit(1);
 }
 
@@ -960,8 +969,10 @@
   const MemoryBuffer &Ref = *Buf.get();
   bool Saved = false;
   std::vector<NewArchiveMember> NewMembers;
+  ParsingMRIScript = true;
 
   for (line_iterator I(Ref, /*SkipBlanks*/ false), E; I != E; ++I) {
+    ++MRILineNumber;
     StringRef Line = *I;
     Line = Line.split(';').first;
     Line = Line.split('*').first;
@@ -1025,8 +1036,10 @@
   }
 
   // Nothing to do if not saved.
-  if (Saved)
+  if (Saved) {
+    ParsingMRIScript = false;
     performOperation(ReplaceOrInsert, &NewMembers);
+  }
   exit(0);
 }
 
Index: llvm/test/tools/llvm-ar/mri-errors.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-ar/mri-errors.test
@@ -0,0 +1,41 @@
+Test different MRI comment formats and white space.
+
+RUN: rm -rf %t && mkdir -p %t
+
+RUN: echo "create %t/mri.ar" > %t/script1.mri
+RUN: echo "addlib %t/missing.a" >> %t/script1.mri
+
+RUN: not llvm-ar -M < %t/script1.mri 2>&1 | FileCheck --check-prefix=SCRIPT1 %s
+SCRIPT1: error: script line 2: Could not open library
+
+RUN: echo "create %t/mri.ar" > %t/script2.mri
+RUN: echo "addlib %t/bad.a" >> %t/script2.mri
+
+RUN: echo "bad archive" > %t/bad.a
+
+RUN: not llvm-ar -M < %t/script2.mri 2>&1 | FileCheck --check-prefix=SCRIPT2 %s
+SCRIPT2: error: script line 2: Could not parse library
+
+RUN: echo "create %t/mri.ar" > %t/script3.mri
+RUN: echo "create %t/second.ar" >> %t/script3.mri
+
+RUN: not llvm-ar -M < %t/script3.mri 2>&1 | FileCheck --check-prefix=SCRIPT3 %s
+SCRIPT3: error: script line 2: Editing multiple archives not supported.
+
+RUN: echo "save" > %t/script4.mri
+RUN: echo "create %t/mri.ar" >> %t/script4.mri
+
+RUN: not llvm-ar -M < %t/script4.mri 2>&1 | FileCheck --check-prefix=SCRIPT4 %s
+SCRIPT4: error: script line 2: File already saved
+
+RUN: echo "create %t/mri.ar" > %t/script5.mri
+RUN: echo "bad command" >> %t/script5.mri
+
+RUN: not llvm-ar -M < %t/script5.mri 2>&1 | FileCheck --check-prefix=SCRIPT5 %s
+SCRIPT5: error: script line 2: Unknown command: bad.
+
+RUN: echo "bad command" | not llvm-ar -M 2>&1 | FileCheck --check-prefix=SCRIPT6 %s
+SCRIPT6: error: script line 1: Unknown command: bad.
+
+RUN: not llvm-ar -M rc %t/mri.ar 2>&1 | FileCheck --check-prefix=SCRIPT7 %s
+SCRIPT7: error: Cannot mix -M and other options


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67449.219719.patch
Type: text/x-patch
Size: 3031 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190911/e79b7340/attachment.bin>


More information about the llvm-commits mailing list