- 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_
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;
}