[cfe-commits] r160452 - in /cfe/trunk: include/clang/AST/Comment.h lib/AST/Comment.cpp

Dmitri Gribenko gribozavr at gmail.com
Wed Jul 18 13:54:33 PDT 2012


Author: gribozavr
Date: Wed Jul 18 15:54:32 2012
New Revision: 160452

URL: http://llvm.org/viewvc/llvm-project?rev=160452&view=rev
Log:
Add caching for TextComment::isWhitespace(), ParagraphComment::isWhitespace().

Modified:
    cfe/trunk/include/clang/AST/Comment.h
    cfe/trunk/lib/AST/Comment.cpp

Modified: cfe/trunk/include/clang/AST/Comment.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Comment.h?rev=160452&r1=160451&r2=160452&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Comment.h (original)
+++ cfe/trunk/include/clang/AST/Comment.h Wed Jul 18 15:54:32 2012
@@ -48,7 +48,20 @@
     /// (There is no separate AST node for a newline.)
     unsigned HasTrailingNewline : 1;
   };
-  enum { NumInlineContentCommentBits = 9 };
+  enum { NumInlineContentCommentBits = NumCommentBits + 1 };
+
+  class TextCommentBitfields {
+    friend class TextComment;
+
+    unsigned : NumInlineContentCommentBits;
+
+    /// True if \c IsWhitespace field contains a valid value.
+    mutable unsigned IsWhitespaceValid : 1;
+
+    /// True if this comment AST node contains only whitespace.
+    mutable unsigned IsWhitespace : 1;
+  };
+  enum { NumTextCommentBits = NumInlineContentCommentBits + 2 };
 
   class HTMLStartTagCommentBitfields {
     friend class HTMLStartTagComment;
@@ -60,6 +73,19 @@
     unsigned IsSelfClosing : 1;
   };
 
+  class ParagraphCommentBitfields {
+    friend class ParagraphComment;
+
+    unsigned : NumCommentBits;
+
+    /// True if \c IsWhitespace field contains a valid value.
+    mutable unsigned IsWhitespaceValid : 1;
+
+    /// True if this comment AST node contains only whitespace.
+    mutable unsigned IsWhitespace : 1;
+  };
+  enum { NumParagraphCommentBits = NumCommentBits + 2 };
+
   class ParamCommandCommentBitfields {
     friend class ParamCommandComment;
 
@@ -76,7 +102,9 @@
   union {
     CommentBitfields CommentBits;
     InlineContentCommentBitfields InlineContentCommentBits;
+    TextCommentBitfields TextCommentBits;
     HTMLStartTagCommentBitfields HTMLStartTagCommentBits;
+    ParagraphCommentBitfields ParagraphCommentBits;
     ParamCommandCommentBitfields ParamCommandCommentBits;
   };
 
@@ -180,8 +208,9 @@
               SourceLocation LocEnd,
               StringRef Text) :
       InlineContentComment(TextCommentKind, LocBegin, LocEnd),
-      Text(Text)
-  { }
+      Text(Text) {
+    TextCommentBits.IsWhitespaceValid = false;
+  }
 
   static bool classof(const Comment *C) {
     return C->getCommentKind() == TextCommentKind;
@@ -195,7 +224,17 @@
 
   StringRef getText() const LLVM_READONLY { return Text; }
 
-  bool isWhitespace() const;
+  bool isWhitespace() const {
+    if (TextCommentBits.IsWhitespaceValid)
+      return TextCommentBits.IsWhitespace;
+
+    TextCommentBits.IsWhitespace = isWhitespaceNoCache();
+    TextCommentBits.IsWhitespaceValid = true;
+    return TextCommentBits.IsWhitespace;
+  }
+
+private:
+  bool isWhitespaceNoCache() const;
 };
 
 /// A command with word-like arguments that is considered inline content.
@@ -442,8 +481,13 @@
                           SourceLocation(),
                           SourceLocation()),
       Content(Content) {
-    if (Content.empty())
+    if (Content.empty()) {
+      ParagraphCommentBits.IsWhitespace = true;
+      ParagraphCommentBits.IsWhitespaceValid = true;
       return;
+    }
+
+    ParagraphCommentBits.IsWhitespaceValid = false;
 
     setSourceRange(SourceRange(Content.front()->getLocStart(),
                                Content.back()->getLocEnd()));
@@ -464,7 +508,17 @@
     return reinterpret_cast<child_iterator>(Content.end());
   }
 
-  bool isWhitespace() const;
+  bool isWhitespace() const {
+    if (ParagraphCommentBits.IsWhitespaceValid)
+      return ParagraphCommentBits.IsWhitespace;
+
+    ParagraphCommentBits.IsWhitespace = isWhitespaceNoCache();
+    ParagraphCommentBits.IsWhitespaceValid = true;
+    return ParagraphCommentBits.IsWhitespace;
+  }
+
+private:
+  bool isWhitespaceNoCache() const;
 };
 
 /// A command that has zero or more word-like arguments (number of word-like

Modified: cfe/trunk/lib/AST/Comment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Comment.cpp?rev=160452&r1=160451&r2=160452&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Comment.cpp (original)
+++ cfe/trunk/lib/AST/Comment.cpp Wed Jul 18 15:54:32 2012
@@ -100,7 +100,7 @@
   llvm_unreachable("Unknown comment kind!");
 }
 
-bool TextComment::isWhitespace() const {
+bool TextComment::isWhitespaceNoCache() const {
   for (StringRef::const_iterator I = Text.begin(), E = Text.end();
        I != E; ++I) {
     const char C = *I;
@@ -111,7 +111,7 @@
   return true;
 }
 
-bool ParagraphComment::isWhitespace() const {
+bool ParagraphComment::isWhitespaceNoCache() const {
   for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) {
     if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
       if (!TC->isWhitespace())





More information about the cfe-commits mailing list