<div>> <span style="background-color:rgb(255,255,255);font-family:arial,sans-serif;font-size:13px">I'm not sure what you mean by "not a File".</span></div>I think he means it in the sense of the Liskov Substitution Principle <<a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">http://en.wikipedia.org/wiki/Liskov_substitution_principle</a>>, not as in lld::FileError literally "does not inherit" from lld::File.<br>
<br>--Sean Silva<div><br><div class="gmail_quote">On Tue, Jun 19, 2012 at 1:51 PM, Nick Kledzik <span dir="ltr"><<a href="mailto:kledzik@apple.com" target="_blank">kledzik@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Jun 18, 2012, at 4:36 PM, Michael Spencer wrote:<br>
>>>><br>
>>>> In asking around for clean models on how to do this better, I've heard two interesting ideas:<br>
>>>><br>
>>>> 1) Incorporate the error_code and string message into the File class.  That is the factory method always returns a File object. But there is a method on File objects to query if they are valid or not.  If they are not, there is a method to get the error_code and/or error string.<br>

><br>
> I like this with some modifications.<br>
><br>
>>>> 2) A variation on this is to have a new subclass of File called FileError.  If the Reader cannot construct a real FileFoo object because of some error, it instead constructs a FileError object and returns that.  The FileError object has methods to get the error_code and/or error string.<br>

><br>
</div>My intuition is the opposite here.  If you add the error information to the lld::File base class then leads you to write the constructor for FileFoo() which does all the parsing, and if it runs into a problem, just sets the error ivar and returns (your FileCOFF is an example of that).  The problem this leads to is half constructed objects.  That can trip up a client which might accidentally call methods on the half constructed object  other than the error getting methods.  It also means the destructor has to be careful when tearing down the half constructed object.<br>

<br>
On the other hand making a new subclass of lld::File called lld::FileError leads you to a different style of programming.  You have to open and parse the file past the point that any error can occur before you construct the FileFoo object.  If anything goes wrong up to the point, you instead construct a FileError object and return that.<br>

<div class="im"><br>
> I dislike this variant because a FileError is not a File.<br>
</div>I'm not sure what you mean by "not a File".  It is a subclass of lld::File.  Its path() method returns the file system path that tried to be used.<br>
<br>
Think of it as a proxy.  The real file could not parsed.  It represents that state.<br>
<div><div class="h5"><br>
<br>
><br>
>>>> With both these ideas the Reader method is:<br>
>>>><br>
>>>>   virtual std::unique_ptr<lld::File> readFile(StringRef path);<br>
>>>><br>
>>>> which is much easier to use.<br>
>>>><br>
>>>><br>
>>>> Any recommendations on a good way to design this?<br>
>>><br>
>>> Would an API like:<br>
>>><br>
>>>   virtual std::unique_ptr<lld::File> readFile(StringRef path, error_code &ErrorInfo);<br>
>><br>
>> That is a common pattern, used for instance in the bit code reader.  If we can unify the OS level error and the syntax/semantic errors from parsing object files, this pattern might work.<br>
>><br>
>> One other thought that leans me towards integrating the error code/msg into the File object is that it works cleaner when you start adding multithreading to the linker.  When you have multiple threads going, each reading an object file and one of them returns an error, you need to cleaning shut down the other threads, and return the error.  With the error status integrated into the File object, all you do is have each thread append it resulting File object onto a list, then after all threads are joined, run through the list once to see if any say they encountered an error.  If the error_code is returned separately from the File, you have to create some data structure to bind together the error_code with which file path could not be parsed.<br>

>><br>
>> -Nick<br>
><br>
> I believe that specifically for object file parsing errors a<br>
> pair<error_code, vector<string> values> (or something similar) as a<br>
> member of File would work. This would be used as:<br>
><br>
> // In the error_category stuff<br>
> class blah_reader_error_category : public llvm::_do_message {<br>
> public:<br>
>  virtual std::string message(int ev) const {<br>
>    ...<br>
>    case uknown_reloc:<br>
>      return "uknown relocation % at %";<br>
>    ...<br>
>  }<br>
> };<br>
><br>
> // In the reader.<br>
><br>
> setError(make_pair(blah_error::unkown_reloc,<br>
> vector<string>{relocationumer, loc})); // loc could be calculated by<br>
> backtracking, or just the current file address. (we could even pass<br>
> the address as a stop address to obj2yaml).<br>
><br>
> // Someplace else.<br>
><br>
> if (File->error())<br>
>  o << File->getErrorMessage();<br>
><br>
> Which would do the string substitution.<br>
</div></div>What is the advantage of this delayed substitution?  The site creating the error needs to convert all values to strings. Your example has two numbers (reloc value and address) which would need to be converted to strings.  It would be simpler all around to just generate the complete string right there at the error site.  Are you trying to support localization? or error numbers?<br>

<br>
Also, are these enum values per file format (i.e. does ELF and mach-o each define their own set)?  Or is there one big set of enums for use by all Readers?<br>
<div class="im"><br>
><br>
> This lets us not spend any time building up a representation for<br>
> diagnostics when none will be printed, and still get decent<br>
> diagnostics with well defined enum values when needed.<br>
<br>
</div>What is the rule of thumb of when a enum value is needed (as opposed to success/failure)?  My thinking is that an enum only makes sense if the client can do something about a specific enum value.  For instance in some future IDE, maybe file_not_found error can prod the IDE to recompile something to regenerate the object file.  Another reason for enums is if you want a way to localize error strings to different languages.  The enums are an index into a per-localization string table.  Given that these error (e.g. unknown relocation) are a bug in the compiler or linker and not a problem the user of the tools can remedy, I'm not sure they need localizing.<br>

<span class="HOEnZb"><font color="#888888"><br>
<br>
-Nick<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</div></div></blockquote></div><br></div>