r349362 - [CodeComplete] Fix test failure on different host and target configs

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 17 08:37:53 PST 2018


Author: ibiryukov
Date: Mon Dec 17 08:37:52 2018
New Revision: 349362

URL: http://llvm.org/viewvc/llvm-project?rev=349362&view=rev
Log:
[CodeComplete] Fix test failure on different host and target configs

This should fix PR40033.

Modified:
    cfe/trunk/unittests/Sema/CodeCompleteTest.cpp

Modified: cfe/trunk/unittests/Sema/CodeCompleteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Sema/CodeCompleteTest.cpp?rev=349362&r1=349361&r2=349362&view=diff
==============================================================================
--- cfe/trunk/unittests/Sema/CodeCompleteTest.cpp (original)
+++ cfe/trunk/unittests/Sema/CodeCompleteTest.cpp Mon Dec 17 08:37:52 2018
@@ -31,6 +31,9 @@ const char TestCCName[] = "test.cc";
 struct CompletionContext {
   std::vector<std::string> VisitedNamespaces;
   std::string PreferredType;
+  // String representation of std::ptrdiff_t on a given platform. This is a hack
+  // to properly account for different configurations of clang.
+  std::string PtrDiffType;
 };
 
 class VisitedContextFinder : public CodeCompleteConsumer {
@@ -47,6 +50,8 @@ public:
     ResultCtx.VisitedNamespaces =
         getVisitedNamespace(Context.getVisitedContexts());
     ResultCtx.PreferredType = Context.getPreferredType().getAsString();
+    ResultCtx.PtrDiffType =
+        S.getASTContext().getPointerDiffType().getAsString();
   }
 
   CodeCompletionAllocator &getAllocator() override {
@@ -133,11 +138,19 @@ CompletionContext runCodeCompleteOnCode(
   return runCompletion(P.Code, P.Points.front());
 }
 
-std::vector<std::string> collectPreferredTypes(StringRef AnnotatedCode) {
+std::vector<std::string>
+collectPreferredTypes(StringRef AnnotatedCode,
+                      std::string *PtrDiffType = nullptr) {
   ParsedAnnotations P = parseAnnotations(AnnotatedCode);
   std::vector<std::string> Types;
-  for (size_t Point : P.Points)
-    Types.push_back(runCompletion(P.Code, Point).PreferredType);
+  for (size_t Point : P.Points) {
+    auto Results = runCompletion(P.Code, Point);
+    if (PtrDiffType) {
+      assert(PtrDiffType->empty() || *PtrDiffType == Results.PtrDiffType);
+      *PtrDiffType = Results.PtrDiffType;
+    }
+    Types.push_back(Results.PreferredType);
+  }
   return Types;
 }
 
@@ -213,9 +226,11 @@ TEST(PreferredTypeTest, BinaryExpr) {
       ptr += ^10;
       ptr -= ^10;
     })cpp";
-  // Expect the normalized ptrdiff_t type, which is typically long or long long.
-  const char *PtrDiff = sizeof(void *) == sizeof(long) ? "long" : "long long";
-  EXPECT_THAT(collectPreferredTypes(Code), Each(PtrDiff));
+  {
+    std::string PtrDiff;
+    auto Types = collectPreferredTypes(Code, &PtrDiff);
+    EXPECT_THAT(Types, Each(PtrDiff));
+  }
 
   // Comparison operators.
   Code = R"cpp(




More information about the cfe-commits mailing list