[cfe-dev] Should we build semantically invalid nodes?

Chris Lattner clattner at apple.com
Thu Oct 23 21:01:24 PDT 2008


On Oct 23, 2008, at 12:50 PM, Doug Gregor wrote:
>> There is certainly some middle ground to tread here, not all  
>> semantic checks
>> will wreak havoc if they allow the expression; I think it's a  
>> worthwhile
>> goal to strive to have an AST as complete as possible, it will be  
>> better in
>> the long run for interesting uses of Clang beyond compiling.
>
> If the AST node that is built is something that is valid for further
> semantic processing (even if it resulted in diagnostics), that's fine.
> So, if we're looking at Sebastian's casting patch, for example, I'm
> fine with generating an AST node for a reinterpret_cast that casts
> away constness, because the resulting expression is still usable for
> type-checking. Anything that requires a "this is an invalid
> expression" flag is, in my opinion, asking for trouble.

There is another option as well: Sema, on detecting an error, can  
force the expression to be something that is not erroneous and return  
that instead.  For example, if the user wrote a cast from an int to a  
function type, it would be fine for sema to report the error and  
"recover" by returning a cast to function pointer.

Note that this should be done only in situations where it is really  
"obvious" what the user intended.  The risk of doing this sort of  
thing is that if it is *not* what the user intended, that you could  
get a cascade of errors complaining about something the user didn't  
write.  This (IMO) is far more damaging to the user experience than  
missing a second error that could have been reported in a subexpression.

-Chris



More information about the cfe-dev mailing list