[PATCH] Refactor MacroInfo for easier access in for-loops

Alexander Kornienko alexfh at google.com
Mon May 25 01:43:32 PDT 2015


On Fri, May 22, 2015 at 10:23 PM, Justin Bogner <mail at justinbogner.com>
wrote:

> Sebastian Edman <sebastian.edman at evidente.se> writes:
> > Hi alexfh, danielmarjamaki,
> >
> > Added possibilty to extract the arguments in a MacroInfo as a
> > container in order to use C++11 style for loops.
> >
> > http://reviews.llvm.org/D9934
> >
> > Files:
> >   include/clang/Lex/MacroInfo.h
> >   lib/Lex/PPMacroExpansion.cpp
> >   lib/Serialization/ASTWriter.cpp
> >
> > Index: include/clang/Lex/MacroInfo.h
> > ===================================================================
> > --- include/clang/Lex/MacroInfo.h
> > +++ include/clang/Lex/MacroInfo.h
> > @@ -182,6 +182,9 @@
> >    bool arg_empty() const { return NumArguments == 0; }
> >    arg_iterator arg_begin() const { return ArgumentList; }
> >    arg_iterator arg_end() const { return ArgumentList+NumArguments; }
> > +  ArrayRef<IdentifierInfo*> args() const {
> > +       return ArrayRef<IdentifierInfo*>(ArgumentList, NumArguments);
> > +  }
>
> It's probably better to use iterator_range from llvm/ADT/iterator_range.h.
>

Can you explain why you think it's better than ArrayRef here?


> Something like:
>
>   llvm::iterator_range<arg_iterator> args() const {
>     llvm::make_range(arg_begin(), arg_end());
>   }
>
> >    unsigned getNumArgs() const { return NumArguments; }
> >
> >    /// \brief Return the argument number of the specified identifier,
> > Index: lib/Serialization/ASTWriter.cpp
> > ===================================================================
> > --- lib/Serialization/ASTWriter.cpp
> > +++ lib/Serialization/ASTWriter.cpp
> > @@ -2135,9 +2135,8 @@
> >        Record.push_back(MI->isGNUVarargs());
> >        Record.push_back(MI->hasCommaPasting());
> >        Record.push_back(MI->getNumArgs());
> > -      for (MacroInfo::arg_iterator I = MI->arg_begin(), E =
> MI->arg_end();
> > -           I != E; ++I)
> > -        AddIdentifierRef(*I, Record);
> > +      for (const auto &I : MI->args())
> > +        AddIdentifierRef(I, Record);
>
> Don't use "I" with iterator ranges - name the variable after what it is
> (Arg, or something like that).
>
> >      }
> >
> >      // If we have a detailed preprocessing record, record the macro
> definition
> > Index: lib/Lex/PPMacroExpansion.cpp
> > ===================================================================
> > --- lib/Lex/PPMacroExpansion.cpp
> > +++ lib/Lex/PPMacroExpansion.cpp
> > @@ -362,9 +362,8 @@
> >
> >    // If this is a function-like macro invocation, it's safe to
> trivially expand
> >    // as long as the identifier is not a macro argument.
> > -  for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
> > -       I != E; ++I)
> > -    if (*I == II)
> > +  for (const auto &I : MI->args())
> > +    if (I == II)
> >        return false;   // Identifier is a macro argument.
> >
> >    return true;
> >
> > EMAIL PREFERENCES
> >   http://reviews.llvm.org/settings/panel/emailpreferences/
> >
> > _______________________________________________
> > cfe-commits mailing list
> > cfe-commits at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150525/7e0fceb8/attachment.html>


More information about the cfe-commits mailing list