[cfe-commits] [PATCH] Compatibility macro detection for the -Wimplicit-fallthrough diagnostic.

Alexander Kornienko reviews at llvm-reviews.chandlerc.com
Fri Sep 28 05:56:05 PDT 2012


  Thanks for the ideas and your implementation! Fixed all this and my tests.


================
Comment at: tools/clang/lib/Sema/AnalysisBasedWarnings.cpp:748
@@ +747,3 @@
+  Tokens.push_back(T);
+  T.setFlag(Token::DisableExpand);
+  Tokens.push_back(T);
----------------
Richard Smith wrote:
> Alexander Kornienko wrote:
> > This hack is used to pass to information, that we need hasLeadingSpace check, to MacroDefinitionEquals.
> > Is there a better way to do this?
> Don't do it at all -- whitespace is permitted between the two '[' tokens which introduce an attribute.
Oh, don't know what made me think the opposite. That makes things a bit simpler. ;)

================
Comment at: tools/clang/lib/Sema/AnalysisBasedWarnings.cpp:743
@@ +742,3 @@
+                                              SourceLocation L) {
+  llvm::SmallVector<Token, 8> Tokens;
+  Token T;
----------------
Richard Smith wrote:
> Instead of building and comparing Tokens, how about a separate data structure to represent what you're matching? Something like...
> 
> class TokenValue {
>   tok::TokenKind Kind;
>   IdentifierInfo *II;
> public:
>   TokenValue(tok::TokenKind Kind) : Kind(Kind), II(0) {}
>   TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {}
>   bool operator==(const Token &Tok) const {
>     return Tok.getKind() == Kind &&
>            (!II || II == Tok.getIdentifierInfo());
> };
> 
> TokenValue ClangFallthroughTokens[] = {
>   tok::l_square, tok::l_square, PP.getIdentifierInfo("clang"),
>   tok::coloncolon, PP.getIdentifierInfo("fallthrough"),
>   tok::r_square, tok::r_square
> };
> 
> // ...
> static bool MacroDefinitionEquals(const MacroInfo *MI, llvm::ArrayRef<TokenValue> Tokens) {
>   return Tokens.size() == MI->getNumTokens() &&
>          std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin());
> }
Yep, that's much better. Stole your code + added a couple of token kind checks.

================
Comment at: tools/clang/include/clang/Basic/TokenKinds.h:69
@@ +68,3 @@
+  return (K == tok::identifier) || (K == tok::raw_identifier);
+}
+
----------------
I've moved these methods here, so they can be used when we only have TokenKind without any Token. Currently this is only a couple of asserts in my code, but overall it seems to be a more appropriate place for this methods.


http://llvm-reviews.chandlerc.com/D50



More information about the cfe-commits mailing list