[PATCH] clang-format: Support PP and Obj-C keywords in 'is'.

strager strager.nds at gmail.com
Mon Jun 29 22:38:53 PDT 2015


I can't update the Phabricator revision, so here's a diff (rebased):

  commit de014c17b0049587a8b5bf4293ce7e5a02230ae3
  Author: Matthew Glazar <strager at fb.com>
  Date:   Mon Jun 29 18:20:56 2015 -0700
  
      clang-format: Support PP and Obj-C keywords in 'is'.
      
      Summary:
      Add overloads of FormatToken::is supporting preprocessor and
      Objective-C token types. This cleans up a bit of code.
      
      Test Plan:
          make check-clang
      
      Reviewers: djasper
      
      Subscribers: curdeius, sas, abdulras, klimek, cfe-commits
      
      Differential Revision: http://reviews.llvm.org/D10420
  
  diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp
  index ed01d68..ce807b4 100644
  --- a/lib/Format/ContinuationIndenter.cpp
  +++ b/lib/Format/ContinuationIndenter.cpp
  @@ -256,8 +256,7 @@ unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
     assert(!State.Stack.empty());
     if ((Current.is(TT_ImplicitStringLiteral) &&
          (Current.Previous->Tok.getIdentifierInfo() == nullptr ||
  -        Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
  -            tok::pp_not_keyword))) {
  +        Current.Previous->is(tok::pp_not_keyword)))) {
       unsigned EndColumn =
           SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd());
       if (Current.LastNewlineOffset != 0) {
  diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
  index 0dd7ca0..ee89fdf 100644
  --- a/lib/Format/Format.cpp
  +++ b/lib/Format/Format.cpp
  @@ -1160,9 +1160,7 @@ private:
         Column = FormatTok->LastLineColumnWidth;
       }
   
  -    if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
  -          Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
  -              tok::pp_define) &&
  +    if (!(Tokens.size() > 0 && Tokens.back()->is(tok::pp_define)) &&
           std::find(ForEachMacros.begin(), ForEachMacros.end(),
                     FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end())
         FormatTok->Type = TT_ForEachMacro;
  diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h
  index 5b7dadb..759924c 100644
  --- a/lib/Format/FormatToken.h
  +++ b/lib/Format/FormatToken.h
  @@ -272,6 +272,13 @@ struct FormatToken {
     bool is(const IdentifierInfo *II) const {
       return II && II == Tok.getIdentifierInfo();
     }
  +  bool is(tok::ObjCKeywordKind Kind) const {
  +    return Tok.getObjCKeywordID() == Kind;
  +  }
  +  bool is(tok::PPKeywordKind Kind) const {
  +    IdentifierInfo *II = Tok.getIdentifierInfo();
  +    return II && II->getPPKeywordID() == Kind;
  +  }
     template <typename A, typename B> bool isOneOf(A K1, B K2) const {
       return is(K1) || is(K2);
     }
  diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
  index 0117f22..5701bb9 100644
  --- a/lib/Format/TokenAnnotator.cpp
  +++ b/lib/Format/TokenAnnotator.cpp
  @@ -681,11 +681,9 @@ public:
       // Directly allow to 'import <string-literal>' to support protocol buffer
       // definitions (code.google.com/p/protobuf) or missing "#" (either way we
       // should not break the line).
  -    IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
       if ((Style.Language == FormatStyle::LK_Java &&
            CurrentToken->is(Keywords.kw_package)) ||
  -        (Info && Info->getPPKeywordID() == tok::pp_import &&
  -         CurrentToken->Next &&
  +        (CurrentToken->is(tok::pp_import) && CurrentToken->Next &&
            CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
                                        tok::kw_static))) {
         next();
  @@ -1751,7 +1749,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
     if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
       return true;
     if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
  -      Left.Tok.getObjCKeywordID() == tok::objc_property)
  +      Left.is(tok::objc_property))
       return true;
     if (Right.is(tok::hashhash))
       return Left.is(tok::hash);
  @@ -1851,7 +1849,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
                Left.is(tok::r_paren)) &&
               Line.Type != LT_PreprocessorDirective);
     }
  -  if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
  +  if (Left.is(tok::at) && !Right.is(tok::objc_not_keyword))
       return false;
     if (Right.is(TT_UnaryOperator))
       return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
  @@ -2136,7 +2134,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
   
     if (Left.is(tok::at))
       return false;
  -  if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
  +  if (Left.is(tok::objc_interface))
       return false;
     if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
       return !Right.is(tok::l_paren);


http://reviews.llvm.org/D10420

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the cfe-commits mailing list