]> granicus.if.org Git - clang/commitdiff
Handle include directive with comments. It turns out that in this case comments...
authorDmitri Gribenko <gribozavr@gmail.com>
Thu, 21 Jun 2012 22:04:37 +0000 (22:04 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Thu, 21 Jun 2012 22:04:37 +0000 (22:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158940 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/RawCommentList.cpp
test/Index/Inputs/annotate-comments-preprocessor.h [new file with mode: 0644]
test/Index/annotate-comments-preprocessor.c [new file with mode: 0644]

index f32e628caa05408bebbe7ed40ea68be6bf5cb576..438fdcd24c7ad2c48cb09a7eb7be153b6fdac01c 100644 (file)
@@ -158,13 +158,15 @@ void RawCommentList::addComment(const RawComment &RC) {
   if (RC.isInvalid())
     return;
 
-  assert((Comments.empty() ||
-          Comments.back().getSourceRange().getEnd() ==
-              RC.getSourceRange().getBegin() ||
-          SourceMgr.isBeforeInTranslationUnit(
-              Comments.back().getSourceRange().getEnd(),
-              RC.getSourceRange().getBegin())) &&
-         "comments are not coming in source order");
+  // Check if the comments are not in source order.
+  while (!Comments.empty() &&
+         !SourceMgr.isBeforeInTranslationUnit(
+              Comments.back().getSourceRange().getBegin(),
+              RC.getSourceRange().getBegin())) {
+    // If they are, just pop a few last comments that don't fit.
+    // This happens if an \#include directive contains comments.
+    Comments.pop_back();
+  }
 
   if (OnlyWhitespaceSeen) {
     if (!onlyWhitespaceBetweenComments(SourceMgr, LastComment, RC))
diff --git a/test/Index/Inputs/annotate-comments-preprocessor.h b/test/Index/Inputs/annotate-comments-preprocessor.h
new file mode 100644 (file)
index 0000000..220f36e
--- /dev/null
@@ -0,0 +1,2 @@
+/* Meow */
+
diff --git a/test/Index/annotate-comments-preprocessor.c b/test/Index/annotate-comments-preprocessor.c
new file mode 100644 (file)
index 0000000..202847f
--- /dev/null
@@ -0,0 +1,45 @@
+// RUN: %clang_cc1 -fsyntax-only -I%S/Inputs %s
+
+// As long as none of this crashes, we don't care about comments in
+// preprocessor directives.
+
+#include "annotate-comments-preprocessor.h" /* Aaa. */ /* Bbb. */
+#include "annotate-comments-preprocessor.h" /* Aaa. */
+#include "annotate-comments-preprocessor.h" /** Aaa. */
+#include "annotate-comments-preprocessor.h" /**< Aaa. */
+#include "annotate-comments-preprocessor.h" // Aaa.
+#include "annotate-comments-preprocessor.h" /// Aaa.
+#include "annotate-comments-preprocessor.h" ///< Aaa.
+
+#define A0 0
+#define A1 1 /* Aaa. */
+#define A2 1 /** Aaa. */
+#define A3 1 /**< Aaa. */
+#define A4 1 // Aaa.
+#define A5 1 /// Aaa.
+#define A6 1 ///< Aaa.
+
+int A[] = { A0, A1, A2, A3, A4, A5, A6 };
+
+#if A0 /** Aaa. */
+int f(int a1[A1], int a2[A2], int a3[A3], int a4[A4], int a5[A5], int a6[A6]);
+#endif /** Aaa. */
+
+#if A1 /** Aaa. */
+int g(int a1[A1], int a2[A2], int a3[A3], int a4[A4], int a5[A5], int a6[A6]);
+#endif /* Aaa. */
+
+#pragma once /** Aaa. */
+
+#define FOO      \
+  do {           \
+    /* Aaa. */   \
+    /** Aaa. */  \
+    /**< Aaa. */ \
+    ;            \
+  } while(0)
+
+void h(void) {
+  FOO;
+}
+