merging of FunctionDecl's

Hi,

example code:

int func(void); (1)
extern int func (void) __asm__ ("FUNC"); (2)

int func(void) {
      return 42;
}

this piece of code has two declarators (1 and 2). When clang decides
what symbol name the code goes like this:

CodeGenModule::GetAddrOfFunction

  return GetOrCreateLLVMFunction(getMangledName(GD), Ty, GD);

the getMangledName() calls

  if (!mangleName(getMangleContext(), ND, Out)) {

which creates CXXNameMangler and tries to mangle the name via it:

    CXXNameMangler Mangler(Context, os);
    if (!Mangler.mangle(cast<NamedDecl>(D->getCanonicalDecl())))
      return false;

the CXXNameMangler::mangle() looks at __asm__(name) and uses it if available

  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
    // If we have an asm name, then we use it as the mangling.
    Out << '\01'; // LLVM IR Marker for __asm("foo")
    Out << ALA->getLabel();
    return true;
  }

the problem is that when emitting function via CodeGenModule::EmitGlobalFunctionDefinition()
only the first FunctionDecl is used. ie. if (1) is first it will ommit
the needed __asm__ name mangling because (1) does not have any AsmLabelAttr assigned.

What is the correct solution for this? What should be done so the (2) has precedence
over (1) ?

thnx! roman

This is actually this issue. We should be mangling based on the declaration we're given, not based on the canonical declaration (which is the first declaration and, therefore, does not have the attribute). Fixed here:

  http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20091026/022863.html

  - Doug

Douglas Gregor wrote:

This is actually this issue. We should be mangling based on the declaration we're given, not based on the canonical declaration (which is the first declaration and, therefore, does not have the attribute). Fixed here:
  
Is there any compelling reason not to push this attribute onto the canonical declaration? I'm not sure what exactly a canonical declaration is if it's not something you can always safely use without fearing loss of critical information.

John.

The canonical declaration is, for function declarations, the first declaration of the function. We don't want to push this attribute onto the canonical declaration because that would change the first declaration of the function, which may not have had that attribute.

  - Doug