[cfe-dev] Assertion failure on invalid code.

Enea Zaffanella zaffanella at cs.unipr.it
Sun Jan 6 09:49:29 PST 2013


Hello.

clang is crashing, in debug mode, on invalid code such as the following
(distilled from a gcc testcase):
======================
template <class>
void foo() {
   (struct S {}*) 0;
}

void bar() {
   foo<int>();
}
======================

$ clang++ -c bug.cc
bug.cc:3:11: error: 'S' can not be defined in a type specifier
   (struct S {}*) 0;
           ^
clang: SemaTemplateInstantiate.cpp:2691: 
llvm::PointerUnion<clang::Decl*, llvm::SmallVector<clang::Decl*, 4u>*>* 
clang::LocalInstantiationScope::findInstantiationOf(const clang::Decl*): 
Assertion `isa<LabelDecl>(D) && "declaration not instantiated in this 
scope"' failed.

The problem seems to be that, after emitting the error diagnostics for 
the template, the code is kept as valid code in the AST; hence it later 
causes a crash when instantiating the template.
A possible fix would be to modify function

static QualType
GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
                              TypeSourceInfo *&ReturnTypeInfo)

so as to flag the offending code just after emitting any error, i.e., 
something like

        SemaRef.Diag(OwnedTagDecl->getLocation(),
               diag::err_type_defined_in_type_specifier)
          << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
+      D.setInvalidType(true);
        break;
      case Declarator::PrototypeContext:
      case Declarator::ObjCParameterContext:

Would that be an acceptable workaround? (note: to be applied in all 
similar places in this function ... there are 4 of them, afaict).
Actually, the type itself has nothing wrong ... the problem is that it 
appears in an illegal context. Are there more appropriate ways to flag 
this code so that it will be later ignored by the template instantiation 
phase?

Enea.



More information about the cfe-dev mailing list