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