r350792 - Refactor declarations of ASTContext allocate functions into its own header.

Richard Trieu via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 9 19:23:25 PST 2019


Author: rtrieu
Date: Wed Jan  9 19:23:25 2019
New Revision: 350792

URL: http://llvm.org/viewvc/llvm-project?rev=350792&view=rev
Log:
Refactor declarations of ASTContext allocate functions into its own header.

Forward declarations of the allocate functions combine with the forward
declaration of the ASTContext class is enough information for some headers
without pulling in ASTContext.h in its entirety.  Pull the existing
declarations from AttrIterator.h into a new header.  Also place the default
alignment size into this header.  Previously, new had its default in
AttrIterator.h while new[] had its default in ASTContext.h.  Add new header
includes where it is needed.  Specifically to ASTVector.h to make it a
standalone header, unlike previously which it was standalone as long as
none of its functions were called.

Added:
    cfe/trunk/include/clang/AST/ASTContextAllocate.h
      - copied, changed from r350683, cfe/trunk/include/clang/AST/AttrIterator.h
Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/ASTVector.h
    cfe/trunk/include/clang/AST/Attr.h
    cfe/trunk/include/clang/AST/AttrIterator.h
    cfe/trunk/include/clang/AST/Decl.h

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=350792&r1=350791&r2=350792&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Wed Jan  9 19:23:25 2019
@@ -15,6 +15,7 @@
 #ifndef LLVM_CLANG_AST_ASTCONTEXT_H
 #define LLVM_CLANG_AST_ASTCONTEXT_H
 
+#include "clang/AST/ASTContextAllocate.h"
 #include "clang/AST/ASTTypeTraits.h"
 #include "clang/AST/CanonicalType.h"
 #include "clang/AST/CommentCommandTraits.h"
@@ -2969,8 +2970,8 @@ inline Selector GetUnarySelector(StringR
 /// This placement form of operator new uses the ASTContext's allocator for
 /// obtaining memory.
 ///
-/// IMPORTANT: These are also declared in clang/AST/AttrIterator.h! Any changes
-/// here need to also be made there.
+/// IMPORTANT: These are also declared in clang/AST/ASTContextAllocate.h!
+/// Any changes here need to also be made there.
 ///
 /// We intentionally avoid using a nothrow specification here so that the calls
 /// to this operator will not perform a null check on the result -- the
@@ -2993,7 +2994,7 @@ inline Selector GetUnarySelector(StringR
 ///                  allocator supports it).
 /// @return The allocated memory. Could be nullptr.
 inline void *operator new(size_t Bytes, const clang::ASTContext &C,
-                          size_t Alignment) {
+                          size_t Alignment /* = 8 */) {
   return C.Allocate(Bytes, Alignment);
 }
 
@@ -3031,7 +3032,7 @@ inline void operator delete(void *Ptr, c
 ///                  allocator supports it).
 /// @return The allocated memory. Could be nullptr.
 inline void *operator new[](size_t Bytes, const clang::ASTContext& C,
-                            size_t Alignment = 8) {
+                            size_t Alignment /* = 8 */) {
   return C.Allocate(Bytes, Alignment);
 }
 

Copied: cfe/trunk/include/clang/AST/ASTContextAllocate.h (from r350683, cfe/trunk/include/clang/AST/AttrIterator.h)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContextAllocate.h?p2=cfe/trunk/include/clang/AST/ASTContextAllocate.h&p1=cfe/trunk/include/clang/AST/AttrIterator.h&r1=350683&r2=350792&rev=350792&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/AttrIterator.h (original)
+++ cfe/trunk/include/clang/AST/ASTContextAllocate.h Wed Jan  9 19:23:25 2019
@@ -1,4 +1,4 @@
-//===- AttrIterator.h - Classes for attribute iteration ---------*- C++ -*-===//
+//===- ASTContextAllocate.h - ASTContext allocate functions -----*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -7,35 +7,27 @@
 //
 //===----------------------------------------------------------------------===//
 //
-//  This file defines the Attr vector and specific_attr_iterator interfaces.
+//  This file declares ASTContext allocation functions separate from the main
+//  code in ASTContext.h.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_AST_ATTRITERATOR_H
-#define LLVM_CLANG_AST_ATTRITERATOR_H
+#ifndef LLVM_CLANG_AST_ASTCONTEXTALLOCATE_H
+#define LLVM_CLANG_AST_ASTCONTEXTALLOCATE_H
 
-#include "clang/Basic/LLVM.h"
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/Support/Casting.h"
-#include <cassert>
 #include <cstddef>
-#include <iterator>
 
 namespace clang {
 
 class ASTContext;
-class Attr;
 
 } // namespace clang
 
 // Defined in ASTContext.h
 void *operator new(size_t Bytes, const clang::ASTContext &C,
                    size_t Alignment = 8);
-
-// FIXME: Being forced to not have a default argument here due to redeclaration
-//        rules on default arguments sucks
 void *operator new[](size_t Bytes, const clang::ASTContext &C,
-                     size_t Alignment);
+                     size_t Alignment = 8);
 
 // It is good practice to pair new/delete operators.  Also, MSVC gives many
 // warnings if a matching delete overload is not declared, even though the
@@ -43,106 +35,4 @@ void *operator new[](size_t Bytes, const
 void operator delete(void *Ptr, const clang::ASTContext &C, size_t);
 void operator delete[](void *Ptr, const clang::ASTContext &C, size_t);
 
-namespace clang {
-
-/// AttrVec - A vector of Attr, which is how they are stored on the AST.
-using AttrVec = SmallVector<Attr *, 4>;
-
-/// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
-/// providing attributes that are of a specific type.
-template <typename SpecificAttr, typename Container = AttrVec>
-class specific_attr_iterator {
-  using Iterator = typename Container::const_iterator;
-
-  /// Current - The current, underlying iterator.
-  /// In order to ensure we don't dereference an invalid iterator unless
-  /// specifically requested, we don't necessarily advance this all the
-  /// way. Instead, we advance it when an operation is requested; if the
-  /// operation is acting on what should be a past-the-end iterator,
-  /// then we offer no guarantees, but this way we do not dereference a
-  /// past-the-end iterator when we move to a past-the-end position.
-  mutable Iterator Current;
-
-  void AdvanceToNext() const {
-    while (!isa<SpecificAttr>(*Current))
-      ++Current;
-  }
-
-  void AdvanceToNext(Iterator I) const {
-    while (Current != I && !isa<SpecificAttr>(*Current))
-      ++Current;
-  }
-
-public:
-  using value_type = SpecificAttr *;
-  using reference = SpecificAttr *;
-  using pointer = SpecificAttr *;
-  using iterator_category = std::forward_iterator_tag;
-  using difference_type = std::ptrdiff_t;
-
-  specific_attr_iterator() = default;
-  explicit specific_attr_iterator(Iterator i) : Current(i) {}
-
-  reference operator*() const {
-    AdvanceToNext();
-    return cast<SpecificAttr>(*Current);
-  }
-  pointer operator->() const {
-    AdvanceToNext();
-    return cast<SpecificAttr>(*Current);
-  }
-
-  specific_attr_iterator& operator++() {
-    ++Current;
-    return *this;
-  }
-  specific_attr_iterator operator++(int) {
-    specific_attr_iterator Tmp(*this);
-    ++(*this);
-    return Tmp;
-  }
-
-  friend bool operator==(specific_attr_iterator Left,
-                         specific_attr_iterator Right) {
-    assert((Left.Current == nullptr) == (Right.Current == nullptr));
-    if (Left.Current < Right.Current)
-      Left.AdvanceToNext(Right.Current);
-    else
-      Right.AdvanceToNext(Left.Current);
-    return Left.Current == Right.Current;
-  }
-  friend bool operator!=(specific_attr_iterator Left,
-                         specific_attr_iterator Right) {
-    return !(Left == Right);
-  }
-};
-
-template <typename SpecificAttr, typename Container>
-inline specific_attr_iterator<SpecificAttr, Container>
-          specific_attr_begin(const Container& container) {
-  return specific_attr_iterator<SpecificAttr, Container>(container.begin());
-}
-template <typename SpecificAttr, typename Container>
-inline specific_attr_iterator<SpecificAttr, Container>
-          specific_attr_end(const Container& container) {
-  return specific_attr_iterator<SpecificAttr, Container>(container.end());
-}
-
-template <typename SpecificAttr, typename Container>
-inline bool hasSpecificAttr(const Container& container) {
-  return specific_attr_begin<SpecificAttr>(container) !=
-          specific_attr_end<SpecificAttr>(container);
-}
-template <typename SpecificAttr, typename Container>
-inline SpecificAttr *getSpecificAttr(const Container& container) {
-  specific_attr_iterator<SpecificAttr, Container> i =
-      specific_attr_begin<SpecificAttr>(container);
-  if (i != specific_attr_end<SpecificAttr>(container))
-    return *i;
-  else
-    return nullptr;
-}
-
-} // namespace clang
-
-#endif // LLVM_CLANG_AST_ATTRITERATOR_H
+#endif // LLVM_CLANG_AST_ASTCONTEXTALLOCATE_H

Modified: cfe/trunk/include/clang/AST/ASTVector.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTVector.h?rev=350792&r1=350791&r2=350792&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTVector.h (original)
+++ cfe/trunk/include/clang/AST/ASTVector.h Wed Jan  9 19:23:25 2019
@@ -18,6 +18,7 @@
 #ifndef LLVM_CLANG_AST_ASTVECTOR_H
 #define LLVM_CLANG_AST_ASTVECTOR_H
 
+#include "clang/AST/ASTContextAllocate.h"
 #include "llvm/ADT/PointerIntPair.h"
 #include <algorithm>
 #include <cassert>

Modified: cfe/trunk/include/clang/AST/Attr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Attr.h?rev=350792&r1=350791&r2=350792&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Wed Jan  9 19:23:25 2019
@@ -14,6 +14,7 @@
 #ifndef LLVM_CLANG_AST_ATTR_H
 #define LLVM_CLANG_AST_ATTR_H
 
+#include "clang/AST/ASTContextAllocate.h"  // For Attrs.inc
 #include "clang/AST/AttrIterator.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/Expr.h"

Modified: cfe/trunk/include/clang/AST/AttrIterator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/AttrIterator.h?rev=350792&r1=350791&r2=350792&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/AttrIterator.h (original)
+++ cfe/trunk/include/clang/AST/AttrIterator.h Wed Jan  9 19:23:25 2019
@@ -26,25 +26,6 @@ namespace clang {
 class ASTContext;
 class Attr;
 
-} // namespace clang
-
-// Defined in ASTContext.h
-void *operator new(size_t Bytes, const clang::ASTContext &C,
-                   size_t Alignment = 8);
-
-// FIXME: Being forced to not have a default argument here due to redeclaration
-//        rules on default arguments sucks
-void *operator new[](size_t Bytes, const clang::ASTContext &C,
-                     size_t Alignment);
-
-// It is good practice to pair new/delete operators.  Also, MSVC gives many
-// warnings if a matching delete overload is not declared, even though the
-// throw() spec guarantees it will not be implicitly called.
-void operator delete(void *Ptr, const clang::ASTContext &C, size_t);
-void operator delete[](void *Ptr, const clang::ASTContext &C, size_t);
-
-namespace clang {
-
 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
 using AttrVec = SmallVector<Attr *, 4>;
 

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=350792&r1=350791&r2=350792&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Wed Jan  9 19:23:25 2019
@@ -15,6 +15,7 @@
 #define LLVM_CLANG_AST_DECL_H
 
 #include "clang/AST/APValue.h"
+#include "clang/AST/ASTContextAllocate.h"
 #include "clang/AST/DeclBase.h"
 #include "clang/AST/DeclarationName.h"
 #include "clang/AST/ExternalASTSource.h"




More information about the cfe-commits mailing list