[PATCH] D82455: [yaml2obj] - Add a way to set default values for macros used in a YAML.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 24 05:54:48 PDT 2020


grimar created this revision.
grimar added reviewers: jhenderson, MaskRay.
Herald added a project: LLVM.

Currently we have to override all macros that are declared. But in many
cases it is convenient to use default values and to override only
a particular one or two.

This provides a way to set a default value for any macro:

  Symbols:
    - Name: [[FOO=foo]]


https://reviews.llvm.org/D82455

Files:
  llvm/test/tools/yaml2obj/macro.yaml
  llvm/tools/yaml2obj/yaml2obj.cpp


Index: llvm/tools/yaml2obj/yaml2obj.cpp
===================================================================
--- llvm/tools/yaml2obj/yaml2obj.cpp
+++ llvm/tools/yaml2obj/yaml2obj.cpp
@@ -75,10 +75,24 @@
     if (Buf.startswith("[[")) {
       size_t I = Buf.find_first_of("[]", 2);
       if (Buf.substr(I).startswith("]]")) {
-        StringRef Macro = Buf.substr(2, I - 2);
+        StringRef MacroExpr = Buf.substr(2, I - 2);
+        StringRef Macro;
+        StringRef Default;
+        std::tie(Macro, Default) = MacroExpr.split('=');
+
+        // When the -D option is requested, we use a value provided.
+        // Otherwise we use a default macro value if any is present
+        // in the YAML description.
         auto It = Defines.find(Macro);
-        if (It != Defines.end()) {
-          Preprocessed += It->second;
+        Optional<StringRef> Value;
+        if (It != Defines.end())
+          Value = It->second;
+        else if (!Default.empty() ||
+                 (Default.empty() && MacroExpr.endswith("=")))
+          Value = Default;
+
+        if (Value) {
+          Preprocessed += *Value;
           Buf = Buf.substr(I + 2);
           continue;
         }
Index: llvm/test/tools/yaml2obj/macro.yaml
===================================================================
--- llvm/test/tools/yaml2obj/macro.yaml
+++ llvm/test/tools/yaml2obj/macro.yaml
@@ -58,3 +58,44 @@
   - Name: "[[a]"
   - Name: "[[a[[a]]"
   - Name: "[[a][[a]][[b]]"
+
+## Check that it is possible to set a default value for a macro in the YAML description.
+# RUN: yaml2obj --docnum=4 %s -o %t4.1
+# RUN: llvm-nm %t4.1 | FileCheck %s --check-prefix=DEFAULT
+
+# DEFAULT: U _foo_
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Symbols:
+  - Name: _[[FOO=foo]]_
+
+## Check that it is possible to override a default macro value.
+# RUN: yaml2obj --docnum=4 %s -o %t4.2 -DFOO=bar
+# RUN: llvm-nm %t4.2 | FileCheck %s --check-prefix=OVERRIDE-DEFAULT
+
+# OVERRIDE-DEFAULT: U _bar_
+
+# RUN: yaml2obj --docnum=4 %s -o %t4.3 -DFOO=bar=foo
+# RUN: llvm-nm %t4.3 | FileCheck %s --check-prefix=OVERRIDE-DEFAULT2
+
+# OVERRIDE-DEFAULT2: U _bar=foo_
+
+## Check that we are able to provide an empty default value for a macro.
+# RUN: yaml2obj --docnum=5 %s -o %t5
+# RUN: llvm-nm %t5 | FileCheck %s --check-prefix=EMPTY-DEFAULT
+
+# EMPTY-DEFAULT: U __
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Symbols:
+  - Name: _[[FOO=]]_


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82455.273003.patch
Type: text/x-patch
Size: 2558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200624/54d48eb5/attachment.bin>


More information about the llvm-commits mailing list