[PATCH] Fix "warning: fallthrough annotation does not directly precede switch label" in lambdas.

Alexander Kornienko alexfh at google.com
Mon Jun 23 13:13:24 PDT 2014


On Mon, Jun 23, 2014 at 7:04 PM, Aaron Ballman <aaron at aaronballman.com>
wrote:

> > Index: lib/Sema/AnalysisBasedWarnings.cpp
> > ===================================================================
> > --- lib/Sema/AnalysisBasedWarnings.cpp
> > +++ lib/Sema/AnalysisBasedWarnings.cpp
> > @@ -1025,6 +1025,9 @@
> >      // methods separately.
> >      bool TraverseDecl(Decl *D) { return true; }
> >
> > +    // We analyze lambda bodies separately. Skip them here.
> > +    bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
> > +
> >    private:
> >
> >      static const AttributedStmt *asFallThroughAttr(const Stmt *S) {
> > @@ -1084,6 +1087,9 @@
> >    if (PerFunction && FM.getFallthroughStmts().empty())
> >      return;
> >
> > +  llvm::errs() << "DiagnoseSwitchLabelsFallthrough: \n";
> > +  AC.getBody()->dumpColor();
>
> This looks like debugging code that should likely go away.
>

Sure, removed.


>
> > +
> >    CFG *Cfg = AC.getCFG();
> >
> >    if (!Cfg)
> > Index: test/SemaCXX/switch-implicit-fallthrough.cpp
> > ===================================================================
> > --- test/SemaCXX/switch-implicit-fallthrough.cpp
> > +++ test/SemaCXX/switch-implicit-fallthrough.cpp
> > @@ -229,25 +229,6 @@
> >    return n;
> >  }
> >
> > -int fallthrough_targets(int n) {
> > -  [[clang::fallthrough]]; // expected-error{{fallthrough annotation is
> outside switch statement}}
> > -
> > -  [[clang::fallthrough]]  // expected-error{{fallthrough attribute is
> only allowed on empty statements}}
> > -  switch (n) {
> > -    case 121:
> > -      n += 400;
> > -      [[clang::fallthrough]]; // no warning here, correct target
> > -    case 123:
> > -      [[clang::fallthrough]]  // expected-error{{fallthrough attribute
> is only allowed on empty statements}}
> > -      n += 800;
> > -      break;
> > -    [[clang::fallthrough]]    // expected-error{{fallthrough attribute
> is only allowed on empty statements}} expected-note{{did you forget ';'?}}
> > -    case 125:
> > -      n += 1600;
> > -  }
> > -  return n;
> > -}
>
> Why did this test move within the file?
>

Clang seems to stop producing warnings after the function containing
errors, so the test with errors should be the last one (or maybe even in a
separate file).


>
> > -
> >  // Fallthrough annotations in local classes used to generate
> "fallthrough
> >  // annotation does not directly precede switch label" warning.
> >  void fallthrough_in_local_class() {
> > @@ -259,12 +240,34 @@
> >            [[clang::fallthrough]]; // no diagnostics
> >          case 1:
> >            x++;
> > +        default: // \
> > +            expected-warning{{unannotated fall-through between switch
> labels}} \
> > +            expected-note{{insert 'break;' to avoid fall-through}}
> >            break;
> >        }
> >      }
> >    };
> >  }
> >
> > +// Fallthrough annotations in lambdas used to generate "fallthrough
> > +// annotation does not directly precede switch label" warning.
> > +void fallthrough_in_lambda() {
> > +  (void)[] {
> > +    int x = 0;
> > +    switch (x) {
> > +    case 0:
> > +      x++;
> > +      [[clang::fallthrough]]; // no diagnostics
> > +    case 1:
> > +      x++;
> > +    default: // \
> > +        expected-warning{{unannotated fall-through between switch
> labels}} \
> > +        expected-note{{insert 'break;' to avoid fall-through}}
> > +      break;
> > +    }
> > +  };
> > +}
> > +
> >  namespace PR18983 {
> >    void fatal() __attribute__((noreturn));
> >    int num();
> > @@ -278,3 +281,22 @@
> >      }
> >    }
> >  }
> > +
> > +int fallthrough_targets(int n) {
> > +  [[clang::fallthrough]]; // expected-error{{fallthrough annotation is
> outside switch statement}}
> > +
> > +  [[clang::fallthrough]]  // expected-error{{fallthrough attribute is
> only allowed on empty statements}}
> > +  switch (n) {
> > +    case 121:
> > +      n += 400;
> > +      [[clang::fallthrough]]; // no warning here, correct target
> > +    case 123:
> > +      [[clang::fallthrough]]  // expected-error{{fallthrough attribute
> is only allowed on empty statements}}
> > +      n += 800;
> > +      break;
> > +    [[clang::fallthrough]]    // expected-error{{fallthrough attribute
> is only allowed on empty statements}} expected-note{{did you forget ';'?}}
> > +    case 125:
> > +      n += 1600;
> > +  }
> > +  return n;
> > +}
> >
>
> ~Aaron
>
> On Mon, Jun 23, 2014 at 11:23 AM, Alexander Kornienko <alexfh at google.com>
> wrote:
> > Hi rsmith,
> >
> > http://reviews.llvm.org/D4258
> >
> > Files:
> >   lib/Sema/AnalysisBasedWarnings.cpp
> >   test/SemaCXX/switch-implicit-fallthrough.cpp
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140623/c8d6f790/attachment.html>


More information about the cfe-commits mailing list