[llvm] 24e12e0 - [LLParser] Print mismatched types in error message

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 21 13:10:59 PDT 2021


Author: Arthur Eubanks
Date: 2021-04-21T13:10:37-07:00
New Revision: 24e12e0726f15dd2002a1091e6a8e50d9a4ebf49

URL: https://github.com/llvm/llvm-project/commit/24e12e0726f15dd2002a1091e6a8e50d9a4ebf49
DIFF: https://github.com/llvm/llvm-project/commit/24e12e0726f15dd2002a1091e6a8e50d9a4ebf49.diff

LOG: [LLParser] Print mismatched types in error message

Helps with debugging invalid handcrafted IR.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D100990

Added: 
    

Modified: 
    llvm/lib/AsmParser/LLParser.cpp
    llvm/test/Assembler/invalid-alias-mismatched-explicit-type.ll
    llvm/test/Assembler/invalid-gep-mismatched-explicit-type.ll
    llvm/test/Assembler/invalid-load-mismatched-explicit-type.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index db2cb66963d93..d56fca1f21fe8 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -931,6 +931,14 @@ static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
     GV.setDSOLocal(true);
 }
 
+static std::string typeComparisonErrorMessage(StringRef Message, Type *Ty1,
+                                              Type *Ty2) {
+  std::string ErrString;
+  raw_string_ostream ErrOS(ErrString);
+  ErrOS << Message << " (" << *Ty1 << " vs " << *Ty2 << ")";
+  return ErrOS.str();
+}
+
 /// parseIndirectSymbol:
 ///   ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
 ///                     OptionalVisibility OptionalDLLStorageClass
@@ -998,13 +1006,18 @@ bool LLParser::parseIndirectSymbol(const std::string &Name, LocTy NameLoc,
     return error(AliaseeLoc, "An alias or ifunc must have pointer type");
   unsigned AddrSpace = PTy->getAddressSpace();
 
-  if (IsAlias && Ty != PTy->getElementType())
-    return error(ExplicitTypeLoc,
-                 "explicit pointee type doesn't match operand's pointee type");
+  if (IsAlias && Ty != PTy->getElementType()) {
+    return error(
+        ExplicitTypeLoc,
+        typeComparisonErrorMessage(
+            "explicit pointee type doesn't match operand's pointee type", Ty,
+            PTy->getElementType()));
+  }
 
-  if (!IsAlias && !PTy->getElementType()->isFunctionTy())
+  if (!IsAlias && !PTy->getElementType()->isFunctionTy()) {
     return error(ExplicitTypeLoc,
                  "explicit pointee type should be a function type");
+  }
 
   GlobalValue *GVal = nullptr;
 
@@ -3844,10 +3857,13 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS) {
 
       Type *BaseType = Elts[0]->getType();
       auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
-      if (Ty != BasePointerType->getElementType())
+      if (Ty != BasePointerType->getElementType()) {
         return error(
             ExplicitTypeLoc,
-            "explicit pointee type doesn't match operand's pointee type");
+            typeComparisonErrorMessage(
+                "explicit pointee type doesn't match operand's pointee type",
+                Ty, BasePointerType->getElementType()));
+      }
 
       unsigned GEPWidth =
           BaseType->isVectorTy()
@@ -7454,9 +7470,13 @@ int LLParser::parseLoad(Instruction *&Inst, PerFunctionState &PFS) {
       Ordering == AtomicOrdering::AcquireRelease)
     return error(Loc, "atomic load cannot use Release ordering");
 
-  if (Ty != cast<PointerType>(Val->getType())->getElementType())
-    return error(ExplicitTypeLoc,
-                 "explicit pointee type doesn't match operand's pointee type");
+  if (Ty != cast<PointerType>(Val->getType())->getElementType()) {
+    return error(
+        ExplicitTypeLoc,
+        typeComparisonErrorMessage(
+            "explicit pointee type doesn't match operand's pointee type", Ty,
+            cast<PointerType>(Val->getType())->getElementType()));
+  }
   SmallPtrSet<Type *, 4> Visited;
   if (!Alignment && !Ty->isSized(&Visited))
     return error(ExplicitTypeLoc, "loading unsized types is not allowed");
@@ -7710,9 +7730,13 @@ int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
   if (!BasePointerType)
     return error(Loc, "base of getelementptr must be a pointer");
 
-  if (Ty != BasePointerType->getElementType())
-    return error(ExplicitTypeLoc,
-                 "explicit pointee type doesn't match operand's pointee type");
+  if (Ty != BasePointerType->getElementType()) {
+    return error(
+        ExplicitTypeLoc,
+        typeComparisonErrorMessage(
+            "explicit pointee type doesn't match operand's pointee type", Ty,
+            BasePointerType->getElementType()));
+  }
 
   SmallVector<Value*, 16> Indices;
   bool AteExtraComma = false;

diff  --git a/llvm/test/Assembler/invalid-alias-mismatched-explicit-type.ll b/llvm/test/Assembler/invalid-alias-mismatched-explicit-type.ll
index d28223793082e..36be49def7ce3 100644
--- a/llvm/test/Assembler/invalid-alias-mismatched-explicit-type.ll
+++ b/llvm/test/Assembler/invalid-alias-mismatched-explicit-type.ll
@@ -1,4 +1,4 @@
 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
-; CHECK: <stdin>:4:12: error: explicit pointee type doesn't match operand's pointee type
+; CHECK: <stdin>:4:12: error: explicit pointee type doesn't match operand's pointee type (i1 vs i2)
 @y = global i2 0
 @x = alias i1, i2* @y

diff  --git a/llvm/test/Assembler/invalid-gep-mismatched-explicit-type.ll b/llvm/test/Assembler/invalid-gep-mismatched-explicit-type.ll
index 1fc88dd43cb30..c9de924384b15 100644
--- a/llvm/test/Assembler/invalid-gep-mismatched-explicit-type.ll
+++ b/llvm/test/Assembler/invalid-gep-mismatched-explicit-type.ll
@@ -1,5 +1,5 @@
 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
-; CHECK: <stdin>:4:22: error: explicit pointee type doesn't match operand's pointee type
+; CHECK: <stdin>:4:22: error: explicit pointee type doesn't match operand's pointee type (i16 vs i32)
 define void @test(i32* %t) {
   %x = getelementptr i16, i32* %t, i32 0
   ret void

diff  --git a/llvm/test/Assembler/invalid-load-mismatched-explicit-type.ll b/llvm/test/Assembler/invalid-load-mismatched-explicit-type.ll
index b8422ed730f2f..f199bea6c333f 100644
--- a/llvm/test/Assembler/invalid-load-mismatched-explicit-type.ll
+++ b/llvm/test/Assembler/invalid-load-mismatched-explicit-type.ll
@@ -1,5 +1,5 @@
 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
-; CHECK: <stdin>:4:13: error: explicit pointee type doesn't match operand's pointee type
+; CHECK: <stdin>:4:13: error: explicit pointee type doesn't match operand's pointee type (i16 vs i32)
 define void @test(i32* %t) {
   %x = load i16, i32* %t
   ret void


        


More information about the llvm-commits mailing list