Looks like init list and compound literal didn't have location test
cases before now, so I threw in four test cases based on the vector
literals.  I'm assuming more test coverage than requested isn't a bad
thing?

Note: also had to edit unittest/AST/MatchVerifier to add OpenCL as a
selectable language, and include/clang/ASTMatcher/ASTMatchers.h to add
compound literals as matchable AST nodes.

--John

On 2/1/13, Richard Smith <[email protected]> wrote:
> This looks fine. Please also provide a new testcase for
> unittests/AST/SourceLocationTest.cpp.
>
> On Fri, Feb 1, 2013 at 5:29 PM, John Stratton
> <[email protected]>wrote:
>
>> Thanks for the feedback.  Here's an updated patch.  To some extent,
>> yes, this situation is weird, but I think the only valid input
>> exercising this code path should are cases where it would have been
>> semantically equivalent to braces anyway.
>>
>> --John
>>
>> On 1/30/13, Richard Smith <[email protected]> wrote:
>> > On Wed, Jan 30, 2013 at 10:57 AM, John Stratton
>> > <[email protected]> wrote:
>> >> Hello.  I've attached a small patch that fixes the 15095 bug I filed a
>> >> couple of days ago, and makes sure that the CompoundLiteral and child
>> >> AST nodes created from a vector literal have correct source-location
>> >> information.  Does this make sense to the veteran Clang developers?
>> >
>> > It's a bit weird that we're building a CompoundLiteralExpr here for a
>> > construct which used parentheses not braces, but given that's our
>> > representation, it seems appropriate to pretend the braces are where
>> > the parens of the initializer were.
>> >
>> > +  if (isVectorLiteral) {
>> > +    SourceLocation litLParenLoc = (PE ? PE->getLParen() :
>> > PLE->getLParenLoc());
>> > +    SourceLocation litRParenLoc = (PE ? PE->getRParen() :
>> > PLE->getRParenLoc());
>> >
>> > Please compute these locations in BuildVectorLiteral instead (within
>> > its 'if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E))' code).
>> >
>> > Also, variable names should start with an uppercase letter (this code
>> > breaks the convention in a few places, but new code should conform).
>> >
>>
>
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h	(revision 174249)
+++ include/clang/ASTMatchers/ASTMatchers.h	(working copy)
@@ -922,6 +922,16 @@
   Stmt,
   UserDefinedLiteral> userDefinedLiteral;
 
+/// \brief Matches compound (i.e. non-scalar) literals
+///
+/// Example match: {1}, (1, 2)
+/// \code
+///   int array[4] = {1}; vector int myvec = (vector int)(1, 2);
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<
+  Stmt,
+  CompoundLiteralExpr> compoundLiteralExpr;
+
 /// \brief Matches nullptr literal.
 const internal::VariadicDynCastAllOfMatcher<
   Stmt,
Index: unittests/AST/SourceLocationTest.cpp
===================================================================
--- unittests/AST/SourceLocationTest.cpp	(revision 174249)
+++ unittests/AST/SourceLocationTest.cpp	(working copy)
@@ -122,5 +122,38 @@
   EXPECT_TRUE(Verifier.match("class C { C(); };", functionDecl()));
 }
 
+TEST(CompoundLiteralExpr, CompoundVectorLiteralRange) {
+  RangeVerifier<CompoundLiteralExpr> Verifier;
+  Verifier.expectRange(2, 11, 2, 22);
+  EXPECT_TRUE(Verifier.match(
+                  "typedef int int2 __attribute__((ext_vector_type(2)));\n"
+                  "int2 i2 = (int2){1, 2};", compoundLiteralExpr()));
+}
+
+TEST(CompoundLiteralExpr, ParensCompoundVectorLiteralRange) {
+  RangeVerifier<CompoundLiteralExpr> Verifier;
+  Verifier.expectRange(2, 11, 2, 22);
+  EXPECT_TRUE(Verifier.match(
+                  "typedef int int2 __attribute__((ext_vector_type(2)));\n"
+                  "int2 i2 = (int2)(1, 2);", 
+                  compoundLiteralExpr(), Lang_OpenCL));
+}
+
+TEST(InitListExpr, VectorLiteralListBraceRange) {
+  RangeVerifier<InitListExpr> Verifier;
+  Verifier.expectRange(2, 17, 2, 22);
+  EXPECT_TRUE(Verifier.match(
+                  "typedef int int2 __attribute__((ext_vector_type(2)));\n"
+                  "int2 i2 = (int2){1, 2};", initListExpr()));
+}
+
+TEST(InitListExpr, VectorLiteralInitListParens) {
+  RangeVerifier<InitListExpr> Verifier;
+  Verifier.expectRange(2, 17, 2, 22);
+  EXPECT_TRUE(Verifier.match(
+                  "typedef int int2 __attribute__((ext_vector_type(2)));\n"
+                  "int2 i2 = (int2)(1, 2);", initListExpr(), Lang_OpenCL));
+}
+
 } // end namespace ast_matchers
 } // end namespace clang
Index: unittests/AST/MatchVerifier.h
===================================================================
--- unittests/AST/MatchVerifier.h	(revision 174249)
+++ unittests/AST/MatchVerifier.h	(working copy)
@@ -25,7 +25,7 @@
 namespace clang {
 namespace ast_matchers {
 
-enum Language { Lang_C, Lang_C89, Lang_CXX };
+enum Language { Lang_C, Lang_C89, Lang_CXX, Lang_OpenCL };
 
 /// \brief Base class for verifying some property of nodes found by a matcher.
 template <typename NodeType>
@@ -85,6 +85,8 @@
     Args.push_back("-std=c++98");
     FileName = "input.cc";
     break;
+  case Lang_OpenCL:
+    FileName = "input.cl";
   }
 
   // Default to failure in case callback is never called
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp	(revision 174249)
+++ lib/Sema/SemaExpr.cpp	(working copy)
@@ -4626,10 +4626,15 @@
   Expr **exprs;
   unsigned numExprs;
   Expr *subExpr;
+  SourceLocation LiteralLParenLoc, LiteralRParenLoc;
   if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) {
+    LiteralLParenLoc = PE->getLParenLoc();
+    LiteralRParenLoc = PE->getRParenLoc();
     exprs = PE->getExprs();
     numExprs = PE->getNumExprs();
-  } else {
+  } else { // isa<ParenExpr> by assertion at function entrance
+    LiteralLParenLoc = cast<ParenExpr>(E)->getLParen();
+    LiteralRParenLoc = cast<ParenExpr>(E)->getRParen();
     subExpr = cast<ParenExpr>(E)->getSubExpr();
     exprs = &subExpr;
     numExprs = 1;
@@ -4686,8 +4691,8 @@
   }
   // FIXME: This means that pretty-printing the final AST will produce curly
   // braces instead of the original commas.
-  InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc,
-                                                   initExprs, RParenLoc);
+  InitListExpr *initE = new (Context) InitListExpr(Context, LiteralLParenLoc,
+                                                   initExprs, LiteralRParenLoc);
   initE->setType(Ty);
   return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE);
 }
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to