3

I am working on a project which uses the LLVM YAML I/O library. This is the documentation/tutorial that I am following:

I am trying to replicate the example where you define a specialization on llvm::yaml::MappingTraits for a struct data type. This example is at the top of the page.

This is my code that I have written:

#include <cstdlib>  /* for EXIT_FAILURE */
#include <string>
#include <vector>

#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/YAMLParser.h"

using std::string;
using std::vector;

using llvm::outs;
using llvm::errs;

using llvm::yaml::ScalarEnumerationTraits;
using llvm::yaml::MappingTraits;
using llvm::yaml::IO;
using llvm::yaml::Input;
using llvm::yaml::Output;

struct Person {
    string name;
    int hatSize;
};

template <>
struct MappingTraits<Person> {
    static void mapping(IO& io, Person& info) {
        io.mapRequired("name", info.name);
        io.mapOptional("hat-size", info.hatSize);
    }
};

int main(int argc, const char **argv) {
    Person tom;
    tom.name = "Tom";
    tom.hatSize = 8;
    Person dan;
    dan.name = "Dan";
    dan.hatSize = 7;
    std::vector<Person> persons;
    persons.push_back(tom);
    persons.push_back(dan);

    Output yout(llvm::outs());
    yout << persons;

    return EXIT_SUCCESS;
}

It seems to me that I have replicated the example code that they have in that tutorial exactly. But when I try to compile the program (using makefile) I get this cryptic error message:

clang++ -I/usr/local/include -std=c++11   -fno-exceptions -fno-rtti -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -std=c++14 -fcxx-exceptions -g -Wall   -c -o yaml_project.o yaml_project.cpp
In file included from yaml_project.cpp:12:
/usr/local/include/llvm/Support/YAMLTraits.h:1871:36: error: implicit instantiation of undefined template 'llvm::yaml::MissingTrait<std::vector<Person, std::allocator<Person> > >'
  char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
                                   ^
yaml_project.cpp:153:10: note: in instantiation of function template specialization 'llvm::yaml::operator<<<std::vector<Person, std::allocator<Person> > >' requested here
    yout << persons;
         ^
/usr/local/include/llvm/Support/YAMLTraits.h:307:8: note: template is declared here
struct MissingTrait;
       ^
1 error generated.
<builtin>: recipe for target 'yaml_project.o' failed
make: *** [yaml_project.o] Error 1

I don't think that the error is in the command that I am using to compile this program, because it has worked for me before to compile and link the LLVM libraries into my executable. I think that the problem is in the code, but I cannot identify what.

The code for the mentioned header file llvm/Support/YAMLTraits.h is here:

https://llvm.org/doxygen/YAMLTraits_8h_source.html

Galaxy
  • 2,363
  • 2
  • 25
  • 59
  • It seems that the problem is with trying to serialize a `vector` of Persons (though it should indeed work _out of the box_). Does it work fine if you try to serialize a single Person? Would help to try locating the problem. – Amir Kirsh Apr 28 '21 at 01:09

1 Answers1

3

Reading the documentation, it seems to me that support for your specific vector<Person> requires registration with a macro:

LLVM_YAML_IS_SEQUENCE_VECTOR(Person)
// or
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(Person)

See, Utility Macros: https://llvm.org/docs/YamlIO.html#id22

Amir Kirsh
  • 12,564
  • 41
  • 74
  • Hello sir. Now I cannot read the yaml file. This is my code: https://ide.geeksforgeeks.org/larG94cSF2 The code compiled successfully but I cannot get it to read the yaml file. This is the error: – Galaxy Apr 28 '21 at 22:42
  • input_file1.yaml:1:1: error: not a mapping - ^ error parsing YAML input from input_file1.yaml Invalid argument – Galaxy Apr 28 '21 at 22:42
  • Seems to be a new problem, better open a new question with your code embedded in the question itself. – Amir Kirsh Apr 29 '21 at 02:48
  • Amir Kirsh Here is my new question: https://stackoverflow.com/q/67324812/5500589 – Galaxy Apr 29 '21 at 20:47