[llvm] r375008 - [Remarks] Add support for prepending a path to external files

Francis Visoiu Mistrih via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 16 08:40:59 PDT 2019


Author: thegameg
Date: Wed Oct 16 08:40:59 2019
New Revision: 375008

URL: http://llvm.org/viewvc/llvm-project?rev=375008&view=rev
Log:
[Remarks] Add support for prepending a path to external files

This helps with testing and debugging for paths that are assumed
absolute.

It also uses a FileError to provide the file path it's trying to open.

Modified:
    llvm/trunk/include/llvm/Remarks/RemarkParser.h
    llvm/trunk/lib/Remarks/BitstreamRemarkParser.cpp
    llvm/trunk/lib/Remarks/BitstreamRemarkParser.h
    llvm/trunk/lib/Remarks/RemarkParser.cpp
    llvm/trunk/lib/Remarks/YAMLRemarkParser.cpp
    llvm/trunk/lib/Remarks/YAMLRemarkParser.h
    llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp

Modified: llvm/trunk/include/llvm/Remarks/RemarkParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Remarks/RemarkParser.h?rev=375008&r1=375007&r2=375008&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Remarks/RemarkParser.h (original)
+++ llvm/trunk/include/llvm/Remarks/RemarkParser.h Wed Oct 16 08:40:59 2019
@@ -39,6 +39,8 @@ public:
 struct RemarkParser {
   /// The format of the parser.
   Format ParserFormat;
+  /// Path to prepend when opening an external remark file.
+  std::string ExternalFilePrependPath;
 
   RemarkParser(Format ParserFormat) : ParserFormat(ParserFormat) {}
 
@@ -82,7 +84,8 @@ createRemarkParser(Format ParserFormat,
 
 Expected<std::unique_ptr<RemarkParser>>
 createRemarkParserFromMeta(Format ParserFormat, StringRef Buf,
-                           Optional<ParsedStringTable> StrTab = None);
+                           Optional<ParsedStringTable> StrTab = None,
+                           Optional<StringRef> ExternalFilePrependPath = None);
 
 } // end namespace remarks
 } // end namespace llvm

Modified: llvm/trunk/lib/Remarks/BitstreamRemarkParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/BitstreamRemarkParser.cpp?rev=375008&r1=375007&r2=375008&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/BitstreamRemarkParser.cpp (original)
+++ llvm/trunk/lib/Remarks/BitstreamRemarkParser.cpp Wed Oct 16 08:40:59 2019
@@ -15,6 +15,7 @@
 #include "BitstreamRemarkParser.h"
 #include "llvm/Remarks/BitstreamRemarkContainer.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
 
 using namespace llvm;
 using namespace llvm::remarks;
@@ -304,8 +305,9 @@ static Error advanceToMetaBlock(Bitstrea
 }
 
 Expected<std::unique_ptr<BitstreamRemarkParser>>
-remarks::createBitstreamParserFromMeta(StringRef Buf,
-                                       Optional<ParsedStringTable> StrTab) {
+remarks::createBitstreamParserFromMeta(
+    StringRef Buf, Optional<ParsedStringTable> StrTab,
+    Optional<StringRef> ExternalFilePrependPath) {
   BitstreamParserHelper Helper(Buf);
   Expected<std::array<char, 4>> Magic = Helper.parseMagic();
   if (!Magic)
@@ -314,9 +316,14 @@ remarks::createBitstreamParserFromMeta(S
   if (Error E = validateMagicNumber(StringRef(Magic->data(), Magic->size())))
     return std::move(E);
 
-  return StrTab
-             ? std::make_unique<BitstreamRemarkParser>(Buf, std::move(*StrTab))
+  auto Parser =
+      StrTab ? std::make_unique<BitstreamRemarkParser>(Buf, std::move(*StrTab))
              : std::make_unique<BitstreamRemarkParser>(Buf);
+
+  if (ExternalFilePrependPath)
+    Parser->ExternalFilePrependPath = *ExternalFilePrependPath;
+
+  return std::move(Parser);
 }
 
 Expected<std::unique_ptr<Remark>> BitstreamRemarkParser::next() {
@@ -409,13 +416,16 @@ Error BitstreamRemarkParser::processExte
         std::make_error_code(std::errc::illegal_byte_sequence),
         "Error while parsing BLOCK_META: missing external file path.");
 
+  SmallString<80> FullPath(ExternalFilePrependPath);
+  sys::path::append(FullPath, *ExternalFilePath);
+
   // External file: open the external file, parse it, check if its metadata
   // matches the one from the separate metadata, then replace the current parser
   // with the one parsing the remarks.
   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
-      MemoryBuffer::getFile(*ExternalFilePath);
+      MemoryBuffer::getFile(FullPath);
   if (std::error_code EC = BufferOrErr.getError())
-    return errorCodeToError(EC);
+    return createFileError(FullPath, EC);
   TmpRemarkBuffer = std::move(*BufferOrErr);
 
   // Create a separate parser used for parsing the separate file.

Modified: llvm/trunk/lib/Remarks/BitstreamRemarkParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/BitstreamRemarkParser.h?rev=375008&r1=375007&r2=375008&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/BitstreamRemarkParser.h (original)
+++ llvm/trunk/lib/Remarks/BitstreamRemarkParser.h Wed Oct 16 08:40:59 2019
@@ -73,9 +73,9 @@ private:
   Error processExternalFilePath(Optional<StringRef> ExternalFilePath);
 };
 
-Expected<std::unique_ptr<BitstreamRemarkParser>>
-createBitstreamParserFromMeta(StringRef Buf,
-                              Optional<ParsedStringTable> StrTab = None);
+Expected<std::unique_ptr<BitstreamRemarkParser>> createBitstreamParserFromMeta(
+    StringRef Buf, Optional<ParsedStringTable> StrTab = None,
+    Optional<StringRef> ExternalFilePrependPath = None);
 
 } // end namespace remarks
 } // end namespace llvm

Modified: llvm/trunk/lib/Remarks/RemarkParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/RemarkParser.cpp?rev=375008&r1=375007&r2=375008&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/RemarkParser.cpp (original)
+++ llvm/trunk/lib/Remarks/RemarkParser.cpp Wed Oct 16 08:40:59 2019
@@ -86,16 +86,19 @@ llvm::remarks::createRemarkParser(Format
 }
 
 Expected<std::unique_ptr<RemarkParser>>
-llvm::remarks::createRemarkParserFromMeta(Format ParserFormat, StringRef Buf,
-                                          Optional<ParsedStringTable> StrTab) {
+llvm::remarks::createRemarkParserFromMeta(
+    Format ParserFormat, StringRef Buf, Optional<ParsedStringTable> StrTab,
+    Optional<StringRef> ExternalFilePrependPath) {
   switch (ParserFormat) {
   // Depending on the metadata, the format can be either yaml or yaml-strtab,
   // regardless of the input argument.
   case Format::YAML:
   case Format::YAMLStrTab:
-    return createYAMLParserFromMeta(Buf, std::move(StrTab));
+    return createYAMLParserFromMeta(Buf, std::move(StrTab),
+                                    std::move(ExternalFilePrependPath));
   case Format::Bitstream:
-    return createBitstreamParserFromMeta(Buf, std::move(StrTab));
+    return createBitstreamParserFromMeta(Buf, std::move(StrTab),
+                                         std::move(ExternalFilePrependPath));
   case Format::Unknown:
     return createStringError(std::make_error_code(std::errc::invalid_argument),
                              "Unknown remark parser format.");

Modified: llvm/trunk/lib/Remarks/YAMLRemarkParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/YAMLRemarkParser.cpp?rev=375008&r1=375007&r2=375008&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/YAMLRemarkParser.cpp (original)
+++ llvm/trunk/lib/Remarks/YAMLRemarkParser.cpp Wed Oct 16 08:40:59 2019
@@ -15,6 +15,7 @@
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Remarks/RemarkParser.h"
 #include "llvm/Support/Endian.h"
+#include "llvm/Support/Path.h"
 
 using namespace llvm;
 using namespace llvm::remarks;
@@ -109,7 +110,8 @@ static Expected<ParsedStringTable> parse
 
 Expected<std::unique_ptr<YAMLRemarkParser>>
 remarks::createYAMLParserFromMeta(StringRef Buf,
-                                  Optional<ParsedStringTable> StrTab) {
+                                  Optional<ParsedStringTable> StrTab,
+                                  Optional<StringRef> ExternalFilePrependPath) {
   // We now have a magic number. The metadata has to be correct.
   Expected<bool> isMeta = parseMagic(Buf);
   if (!isMeta)
@@ -138,11 +140,17 @@ remarks::createYAMLParserFromMeta(String
     // If it starts with "---", there is no external file.
     if (!Buf.startswith("---")) {
       // At this point, we expect Buf to contain the external file path.
+      StringRef ExternalFilePath = Buf;
+      SmallString<80> FullPath;
+      if (ExternalFilePrependPath)
+        FullPath = *ExternalFilePrependPath;
+      sys::path::append(FullPath, ExternalFilePath);
+
       // Try to open the file and start parsing from there.
       ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
-          MemoryBuffer::getFile(Buf);
+          MemoryBuffer::getFile(FullPath);
       if (std::error_code EC = BufferOrErr.getError())
-        return errorCodeToError(EC);
+        return createFileError(FullPath, EC);
 
       // Keep the buffer alive.
       SeparateBuf = std::move(*BufferOrErr);

Modified: llvm/trunk/lib/Remarks/YAMLRemarkParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/YAMLRemarkParser.h?rev=375008&r1=375007&r2=375008&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/YAMLRemarkParser.h (original)
+++ llvm/trunk/lib/Remarks/YAMLRemarkParser.h Wed Oct 16 08:40:59 2019
@@ -111,7 +111,8 @@ protected:
 
 Expected<std::unique_ptr<YAMLRemarkParser>>
 createYAMLParserFromMeta(StringRef Buf,
-                         Optional<ParsedStringTable> StrTab = None);
+                         Optional<ParsedStringTable> StrTab = None,
+                         Optional<StringRef> ExternalFilePrependPath = None);
 
 } // end namespace remarks
 } // end namespace llvm

Modified: llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp?rev=375008&r1=375007&r2=375008&view=diff
==============================================================================
--- llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp (original)
+++ llvm/trunk/unittests/Remarks/YAMLRemarksParsingTest.cpp Wed Oct 16 08:40:59 2019
@@ -63,12 +63,15 @@ bool parseExpectError(const char (&Buf)[
   return StringRef(Stream.str()).contains(Error);
 }
 
-void parseExpectErrorMeta(StringRef Buf, const char *Error) {
+void parseExpectErrorMeta(StringRef Buf, const char *Error,
+                          Optional<StringRef> ExternalFilePrependPath = None) {
   std::string ErrorStr;
   raw_string_ostream Stream(ErrorStr);
 
   Expected<std::unique_ptr<remarks::RemarkParser>> MaybeParser =
-      remarks::createRemarkParserFromMeta(remarks::Format::YAML, Buf);
+      remarks::createRemarkParserFromMeta(remarks::Format::YAML, Buf,
+                                          /*StrTab=*/None,
+                                          std::move(ExternalFilePrependPath));
   handleAllErrors(MaybeParser.takeError(),
                   [&](const ErrorInfoBase &EIB) { EIB.log(Stream); });
 
@@ -705,6 +708,14 @@ TEST(YAMLRemarks, ParsingBadMeta) {
                                  "\0\0\0\0\0\0\0\0"
                                  "\0\0\0\0\0\0\0\0"
                                  "/path/",
-                                 28),
-                       "No such file or directory");
+                                 30),
+                       "'/path/': No such file or directory");
+
+  parseExpectErrorMeta(StringRef("REMARKS\0"
+                                 "\0\0\0\0\0\0\0\0"
+                                 "\0\0\0\0\0\0\0\0"
+                                 "/path/",
+                                 30),
+                       "'/baddir/path/': No such file or directory",
+                       StringRef("/baddir/"));
 }




More information about the llvm-commits mailing list