[llvm] r194655 - Add dyn_cast<> support to YAML I/O's IO class

Nick Kledzik kledzik at apple.com
Thu Nov 14 11:09:33 PST 2013


Sean,

I’m going to re-think this.  I’m going to see if there is a clean way to add a validate() method that is a sibling to the mapping() method.  If it exists, YAML I/O will call it to check for semantic errors.  When reading yaml, it will be called after the mapping has been read into the struct.  When writing yaml, it will be called before the map is written to check for an invalid in-memory struct.

-Nick

On Nov 13, 2013, at 7:41 PM, Nick Kledzik <kledzik at apple.com> wrote:
> This is a stepping stone to supporting some semantic level checking which is only relevant when doing input.    Yes, I cringe too when the Input/Output symmetry is broken.  
> 
> In particular, I’m working on a way to encode mach-o as yaml for test cases.  I need to handle semantic errors like an invalid symbol index in a relocation.  I’ll need to only do the checks when inputting and then set an error_code just as if there had been a syntax error in the yaml.
> 
> -Nick
> 
> On Nov 13, 2013, at 7:30 PM, Sean Silva <silvas at purdue.edu> wrote:
>> Out of curiousity what is use case for this? One of the things I like about YAMLIO is that both input and output "work from a single point of truth"; being able to distinguish when we are inputting from when we are outputting seems like it could be problematic as it circumvents that property of the interface.
>> 
>> -- Sean Silva
>> 
>> 
>> On Wed, Nov 13, 2013 at 9:38 PM, Nick Kledzik <kledzik at apple.com> wrote:
>> Author: kledzik
>> Date: Wed Nov 13 20:38:07 2013
>> New Revision: 194655
>> 
>> URL: http://llvm.org/viewvc/llvm-project?rev=194655&view=rev
>> Log:
>> Add dyn_cast<> support to YAML I/O's IO class
>> 
>> Modified:
>>     llvm/trunk/include/llvm/Support/YAMLTraits.h
>>     llvm/trunk/lib/Support/YAMLTraits.cpp
>>     llvm/trunk/unittests/Support/YAMLIOTest.cpp
>> 
>> Modified: llvm/trunk/include/llvm/Support/YAMLTraits.h
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLTraits.h?rev=194655&r1=194654&r2=194655&view=diff
>> ==============================================================================
>> --- llvm/trunk/include/llvm/Support/YAMLTraits.h (original)
>> +++ llvm/trunk/include/llvm/Support/YAMLTraits.h Wed Nov 13 20:38:07 2013
>> @@ -18,6 +18,7 @@
>>  #include "llvm/ADT/StringRef.h"
>>  #include "llvm/ADT/StringSwitch.h"
>>  #include "llvm/ADT/Twine.h"
>> +#include "llvm/Support/Casting.h"
>>  #include "llvm/Support/Compiler.h"
>>  #include "llvm/Support/SourceMgr.h"
>>  #include "llvm/Support/YAMLParser.h"
>> @@ -317,7 +318,7 @@ public:
>>    IO(void *Ctxt=NULL);
>>    virtual ~IO();
>> 
>> -  virtual bool outputting() = 0;
>> +  virtual bool outputting() const = 0;
>> 
>>    virtual unsigned beginSequence() = 0;
>>    virtual bool preflightElement(unsigned, void *&) = 0;
>> @@ -694,8 +695,10 @@ public:
>>    // To set alternate error reporting.
>>    void setDiagHandler(llvm::SourceMgr::DiagHandlerTy Handler, void *Ctxt = 0);
>> 
>> +  static bool classof(const IO *io) { return !io->outputting(); }
>> +
>>  private:
>> -  virtual bool outputting();
>> +  virtual bool outputting() const;
>>    virtual bool mapTag(StringRef, bool);
>>    virtual void beginMapping();
>>    virtual void endMapping();
>> @@ -819,7 +822,9 @@ public:
>>    Output(llvm::raw_ostream &, void *Ctxt=NULL);
>>    virtual ~Output();
>> 
>> -  virtual bool outputting();
>> +  static bool classof(const IO *io) { return io->outputting(); }
>> +
>> +  virtual bool outputting() const;
>>    virtual bool mapTag(StringRef, bool);
>>    virtual void beginMapping();
>>    virtual void endMapping();
>> 
>> Modified: llvm/trunk/lib/Support/YAMLTraits.cpp
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLTraits.cpp?rev=194655&r1=194654&r2=194655&view=diff
>> ==============================================================================
>> --- llvm/trunk/lib/Support/YAMLTraits.cpp (original)
>> +++ llvm/trunk/lib/Support/YAMLTraits.cpp Wed Nov 13 20:38:07 2013
>> @@ -59,7 +59,7 @@ void Input::setDiagHandler(SourceMgr::Di
>>    SrcMgr.setDiagHandler(Handler, Ctxt);
>>  }
>> 
>> -bool Input::outputting() {
>> +bool Input::outputting() const {
>>    return false;
>>  }
>> 
>> @@ -382,7 +382,7 @@ Output::Output(raw_ostream &yout, void *
>>  Output::~Output() {
>>  }
>> 
>> -bool Output::outputting() {
>> +bool Output::outputting() const {
>>    return true;
>>  }
>> 
>> 
>> Modified: llvm/trunk/unittests/Support/YAMLIOTest.cpp
>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLIOTest.cpp?rev=194655&r1=194654&r2=194655&view=diff
>> ==============================================================================
>> --- llvm/trunk/unittests/Support/YAMLIOTest.cpp (original)
>> +++ llvm/trunk/unittests/Support/YAMLIOTest.cpp Wed Nov 13 20:38:07 2013
>> @@ -1074,6 +1074,76 @@ TEST(YAMLIO, TestTaggedDocumentsWriteAnd
>>  }
>> 
>> 
>> +//===----------------------------------------------------------------------===//
>> +//  Test dyn_cast<> on IO object
>> +//===----------------------------------------------------------------------===//
>> +
>> +struct DynCast {
>> +  int value;
>> +};
>> +typedef std::vector<DynCast> DynCastSequence;
>> +
>> +LLVM_YAML_IS_SEQUENCE_VECTOR(DynCast)
>> +
>> +namespace llvm {
>> +namespace yaml {
>> +  template <>
>> +  struct MappingTraits<DynCast> {
>> +    static void mapping(IO &io, DynCast& info) {
>> +      // Change 10 to 13 when writing yaml.
>> +      if (Output *output = dyn_cast<Output>(&io)) {
>> +        (void)output;
>> +        if (info.value == 10)
>> +          info.value = 13;
>> +      }
>> +      io.mapRequired("value", info.value);
>> +      // Change 20 to 23 when parsing yaml.
>> +      if (Input *input = dyn_cast<Input>(&io)) {
>> +        (void)input;
>> +        if (info.value == 20)
>> +          info.value = 23;
>> +      }
>> +    }
>> +  };
>> +}
>> +}
>> +
>> +//
>> +// Test writing then reading back a sequence of mappings
>> +//
>> +TEST(YAMLIO, TestDynCast) {
>> +  std::string intermediate;
>> +  {
>> +    DynCast entry1;
>> +    entry1.value = 10;
>> +    DynCast entry2;
>> +    entry2.value = 20;
>> +    DynCast entry3;
>> +    entry3.value = 30;
>> +    DynCastSequence seq;
>> +    seq.push_back(entry1);
>> +    seq.push_back(entry2);
>> +    seq.push_back(entry3);
>> +
>> +    llvm::raw_string_ostream ostr(intermediate);
>> +    Output yout(ostr);
>> +    yout << seq;
>> +  }
>> +
>> +  {
>> +    Input yin(intermediate);
>> +    DynCastSequence seq2;
>> +    yin >> seq2;
>> +
>> +    EXPECT_FALSE(yin.error());
>> +    EXPECT_EQ(seq2.size(), 3UL);
>> +    EXPECT_EQ(seq2[0].value, 13);   // Verify changed to 13.
>> +    EXPECT_EQ(seq2[1].value, 23);   // Verify changed to 23.
>> +    EXPECT_EQ(seq2[2].value, 30);   // Verify stays same.
>> +  }
>> +}
>> +
>> +
>> 
>>  //===----------------------------------------------------------------------===//
>>  //  Test error handling
>> 
>> 
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131114/97f7b951/attachment.html>


More information about the llvm-commits mailing list