[cfe-commits] [PATCH] Implicit fall-through between switch labels

Richard Smith richard at metafoo.co.uk
Mon Apr 30 20:28:58 PDT 2012

Hi Alex,

The 'unreachable block' test isn't quite right; a block can have
(unreachable) predecessors and still be unreachable:

switch (x) {
// oops, deleted "case 0:"
  if (a)
  [[clang::fallthrough]]; // no warning here!
case 1:

That said, since -Wunreachable-code warns on (most) such cases, I don't
think that is necessarily a problem. It might be worth updating the comment
in the code to indicate that we only catch trivial cases, though.

Style nits: Please update the capitalization of FallthroughMapper's members
and local variables to match the coding standard. Functions should start
with a lowercase letter, and variables and data members should start with a
capital letter. Also, please start comments with a capital letter, and end
them with a full stop when they are full sentences.

On Mon, Apr 30, 2012 at 5:40 AM, Alexander Kornienko <alexfh at google.com>wrote:

> On Fri, Apr 27, 2012 at 3:47 AM, Jordy Rose <jediknil at belkadan.com> wrote:
>> >> FWIW, Richard convinced me that this must be spelled
>> 'clang::fallthrough' for the immediate future. We should probably focus on
>> that form.
>> >
>> > Done. It turned out though, that namespaces for attributes were not
>> used until now, so I implemented my own vision: I just add namespace  as a
>> prefix to spelling with triple underscore delimiter. Better ideas?
>> The right thing might be to implement real namespacing now, but why not
>> just use ::, as in the written form? The parser only expects attributes to
>> be made of identifiers, but our table can use any characters we want.
> These strings are used as parts of C++ identifiers in clang code, so we
> can't use arbitrary characters inside. I found underscores to be the most
> appropriate option here. Regarding correct implementation of namespacing in
> attributes, I strongly believe it's an independent piece of functionality,
> which requires a separate design discussion. So I would leave my temporary
> implementation (which is quite flexible to be used at the moment).

Since you're planning on working on fixing this next, I think the temporary
implementation is OK. Please add a FIXME next to it, though.

> > +    typedef std::vector<const Stmt*> StmtVec;
> > +    bool GetFallThroughSourceStmts(const CFGBlock &B, StmtVec
>> &Unannotated,
>> > +                                   StmtVec &Annotated) {
>> Should this be a SmallVector? The argument would then be a
>> SmallVectorImpl<const Stmt*>, and only the actual stack-allocated vector
>> needs to have a size.
>> Also, do we need the annotated set? If not, we could leave it out for
>> now, and only put it back in if we want to try for some intelligence in the
>> suggestions.
> I changed this to return just counts of annotated and unannotated
> fall-through source locations

I notice you don't use UnannotatedCnt outside GetFallThroughSourceCount. I
suggest you drop that argument, and replace UnannotatedCnt with a bool
inside the function. AnnotatedCnt could likewise be replaced by a bool
FoundAnyFallthroughAttributes or similar.

I'd also suggest renaming that function, perhaps to

> > +      SourceLocation L = Label->getLocStart();
>> > +      S.Diag(L, diag::note_insert_fallthrough_fixit) <<
>> > +        FixItHint::CreateInsertion(L, "[[clang::fallthrough]];");
>> This will give a proper diagnostic, but not a very nice placement of the
>> attribute. Clients like Xcode actually apply fixit hints to the user's
>> code, and really we want this on a previous line. Can we figure out how to
>> insert this in the right place, rather than suggest something the user will
>> have to manually edit afterwards?
> Proper formatting should be a function of an IDE, not a compiler. If Xcode
> uses clang's fix-it hints for changing code, it could also deal with
> white-space handling.
> I've inserted a line-break at the end of fix-it hint, but I'm not really
> sure if even this is a duty of the compiler. And AFAIK there are no other
> examples of fix-it hints, which insert new statements or deal with code
> formatting in any other way.

We do have existing fixits which insert spaces and newlines to keep the
code tidy, in cases where "tidy" is obvious, but I agree that it can't be
the responsibility of the fixit to reformat the code according to whatever
coding style is relevant. This fixit seems like a bit of a grey area, since
"keep the case label at the same indentation, and put the attribute at the
same indentation as the previous line" is likely to cover almost all cases
perfectly, but I think what you've got now is a reasonable tradeoff. And I
don't want for us to need to worry about what happens if 'case' or
'default' isn't the first token on the line :-)

Other than the superficial issues above, I think this is looking good.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120430/c282442b/attachment.html>

More information about the cfe-commits mailing list