[llvm] 657c4ab - [yaml2obj] - Add a way to set default values for macros used in a YAML.
Georgii Rymar via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 30 02:12:05 PDT 2020
Author: Georgii Rymar
Date: 2020-06-30T12:05:30+03:00
New Revision: 657c4ab39dc80f569f422d40347b97586c27b2ba
URL: https://github.com/llvm/llvm-project/commit/657c4ab39dc80f569f422d40347b97586c27b2ba
DIFF: https://github.com/llvm/llvm-project/commit/657c4ab39dc80f569f422d40347b97586c27b2ba.diff
LOG: [yaml2obj] - Add a way to set default values for macros used in a YAML.
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]]
```
Differential revision: https://reviews.llvm.org/D82455
Added:
Modified:
llvm/test/tools/yaml2obj/macro.yaml
llvm/tools/yaml2obj/yaml2obj.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/yaml2obj/macro.yaml b/llvm/test/tools/yaml2obj/macro.yaml
index d7834fa6d97a..b299dc9f7f97 100644
--- a/llvm/test/tools/yaml2obj/macro.yaml
+++ b/llvm/test/tools/yaml2obj/macro.yaml
@@ -58,3 +58,34 @@ Symbols:
- 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.
+## Check that we are able to provide an empty default value for a macro.
+# RUN: yaml2obj --docnum=4 %s -o %t4.1
+# RUN: llvm-nm --no-sort %t4.1 | FileCheck %s --check-prefix=DEFAULT
+
+# DEFAULT: U _foo_
+# DEFAULT-NEXT: U __
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Symbols:
+ - Name: _[[FOO=foo]]_
+ - Name: _[[BAR=]]_
+
+## Check that it is possible to override a default macro value.
+# RUN: yaml2obj --docnum=4 %s -o %t4.2 -DFOO=bar -DBAR=foo
+# RUN: llvm-nm --no-sort %t4.2 | FileCheck %s --check-prefix=OVERRIDE-DEFAULT1
+
+# OVERRIDE-DEFAULT1: U _bar_
+# OVERRIDE-DEFAULT1-NEXT: U _foo_
+
+# RUN: yaml2obj --docnum=4 %s -o %t4.3 -DFOO=bar=foo -DBAR=foo=bar
+# RUN: llvm-nm --no-sort %t4.3 | FileCheck %s --check-prefix=OVERRIDE-DEFAULT2
+
+# OVERRIDE-DEFAULT2: U _bar=foo_
+# OVERRIDE-DEFAULT2-NEXT: U _foo=bar_
diff --git a/llvm/tools/yaml2obj/yaml2obj.cpp b/llvm/tools/yaml2obj/yaml2obj.cpp
index 214416da0366..d18b2c17512b 100644
--- a/llvm/tools/yaml2obj/yaml2obj.cpp
+++ b/llvm/tools/yaml2obj/yaml2obj.cpp
@@ -75,10 +75,22 @@ static Optional<std::string> preprocess(StringRef Buf,
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 the provided value.
+ // Otherwise we use a default macro value if present.
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() || MacroExpr.endswith("="))
+ Value = Default;
+
+ if (Value) {
+ Preprocessed += *Value;
Buf = Buf.substr(I + 2);
continue;
}
More information about the llvm-commits
mailing list