[llvm] 07e6eb6 - [yaml2obj] Add a `-E` flag to preprocess only.

Simon Tatham via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 2 05:58:29 PDT 2022


Author: Simon Tatham
Date: 2022-08-02T13:56:27+01:00
New Revision: 07e6eb6e75b8ab65cd54346790dd53862c7d5847

URL: https://github.com/llvm/llvm-project/commit/07e6eb6e75b8ab65cd54346790dd53862c7d5847
DIFF: https://github.com/llvm/llvm-project/commit/07e6eb6e75b8ab65cd54346790dd53862c7d5847.diff

LOG: [yaml2obj] Add a `-E` flag to preprocess only.

If you're having trouble getting a yaml2obj macro expansion to do what
you want, it's useful to be able to print the output of the
preprocessing to see what your macros expanded to //before// going
into the YAML processing phase.

yaml2obj has its own preprocessing system which isn't the same as any
other well-known thing like cpp. So there's no way to do this macro
expansion via another tool: yaml2obj will have to do it itself.

In this commit I add an `-E` flag to yaml2obj to do that.

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

Added: 
    llvm/test/tools/yaml2obj/preprocess-only.test

Modified: 
    llvm/tools/yaml2obj/yaml2obj.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/yaml2obj/preprocess-only.test b/llvm/test/tools/yaml2obj/preprocess-only.test
new file mode 100644
index 0000000000000..12fcb45d72a65
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/preprocess-only.test
@@ -0,0 +1,16 @@
+# RUN: yaml2obj -E -Dfoo=wibble %s | FileCheck %s
+
+This is a test of yaml2obj's pure preprocessing mode, so it doesn't
+have to contain valid YAML, or any YAML at all. But we do have to be
+careful with the FileCheck CHECK directives, because they'll be
+emitted into the preprocessed output, and risk matching themselves!
+For that reason, each one matches only at the start of a line.
+
+Expand a macro:
+[[foo]]            # CHECK: {{^wibble}}
+
+Expand an undefined macro:
+[[bar]]            # CHECK: {{^\[\[bar\]\]}}
+
+Expand an undefined macro where we provided a default value:
+[[baz=123]]        # CHECK: {{^123}}

diff  --git a/llvm/tools/yaml2obj/yaml2obj.cpp b/llvm/tools/yaml2obj/yaml2obj.cpp
index 8a44c44eedc0a..1d6d3b5185ed0 100644
--- a/llvm/tools/yaml2obj/yaml2obj.cpp
+++ b/llvm/tools/yaml2obj/yaml2obj.cpp
@@ -40,6 +40,9 @@ cl::list<std::string>
                "definition. The syntax is <macro>=<definition>"),
       cl::cat(Cat));
 
+cl::opt<bool> PreprocessOnly("E", cl::desc("Just print the preprocessed file"),
+                             cl::cat(Cat));
+
 cl::opt<unsigned>
     DocNum("docnum", cl::init(1),
            cl::desc("Read specified document from input (default = 1)"),
@@ -133,11 +136,16 @@ int main(int argc, char **argv) {
   Optional<std::string> Buffer = preprocess(Buf.get()->getBuffer(), ErrHandler);
   if (!Buffer)
     return 1;
-  yaml::Input YIn(*Buffer);
 
-  if (!convertYAML(YIn, Out->os(), ErrHandler, DocNum,
-                   MaxSize == 0 ? UINT64_MAX : MaxSize))
-    return 1;
+  if (PreprocessOnly) {
+    Out->os() << Buffer;
+  } else {
+    yaml::Input YIn(*Buffer);
+
+    if (!convertYAML(YIn, Out->os(), ErrHandler, DocNum,
+                     MaxSize == 0 ? UINT64_MAX : MaxSize))
+      return 1;
+  }
 
   Out->keep();
   Out->os().flush();


        


More information about the llvm-commits mailing list