X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=docs%2FYamlIO.rst;h=b1917b6469e889b1206dbebcab400310aacbd21a;hb=36b56886974eae4f9c5ebc96befd3e7bfe5de338;hp=3ecd03afb24d8871d2dd51f085cfa9806ff865bc;hpb=69a8640022b04415ae9fac62f8ab090601d8f889;p=android-x86%2Fexternal-llvm.git diff --git a/docs/YamlIO.rst b/docs/YamlIO.rst index 3ecd03afb24..b1917b6469e 100644 --- a/docs/YamlIO.rst +++ b/docs/YamlIO.rst @@ -234,6 +234,7 @@ The following types have built-in support in YAML I/O: * float * double * StringRef +* std::string * int64_t * int32_t * int16_t @@ -640,12 +641,50 @@ The YAML syntax supports tags as a way to specify the type of a node before it is parsed. This allows dynamic types of nodes. But the YAML I/O model uses static typing, so there are limits to how you can use tags with the YAML I/O model. Recently, we added support to YAML I/O for checking/setting the optional -tag on a map. Using this functionality it is even possbile to support differnt +tag on a map. Using this functionality it is even possbile to support different mappings, as long as they are convertable. To check a tag, inside your mapping() method you can use io.mapTag() to specify what the tag should be. This will also add that tag when writing yaml. +Validation +---------- + +Sometimes in a yaml map, each key/value pair is valid, but the combination is +not. This is similar to something having no syntax errors, but still having +semantic errors. To support semantic level checking, YAML I/O allows +an optional ``validate()`` method in a MappingTraits template specialization. + +When parsing yaml, the ``validate()`` method is call *after* all key/values in +the map have been processed. Any error message returned by the ``validate()`` +method during input will be printed just a like a syntax error would be printed. +When writing yaml, the ``validate()`` method is called *before* the yaml +key/values are written. Any error during output will trigger an ``assert()`` +because it is a programming error to have invalid struct values. + + +.. code-block:: c++ + + using llvm::yaml::MappingTraits; + using llvm::yaml::IO; + + struct Stuff { + ... + }; + + template <> + struct MappingTraits { + static void mapping(IO &io, Stuff &stuff) { + ... + } + static StringRef validate(IO &io, Stuff &stuff) { + // Look at all fields in 'stuff' and if there + // are any bad values return a string describing + // the error. Otherwise return an empty string. + return StringRef(); + } + }; + Sequence ========