Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect source location for builtin functions in the clang AST. #61839

Open
hokein opened this issue Mar 30, 2023 · 3 comments
Open

incorrect source location for builtin functions in the clang AST. #61839

hokein opened this issue Mar 30, 2023 · 3 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@hokein
Copy link
Collaborator

hokein commented Mar 30, 2023

Given the following case:

int test(unsigned __x)  { 
  return __builtin_popcount(1); // line 2
}
int test2(unsigned __x)  { 
  return __builtin_popcount(1);
}

void foo() {
  __builtin^_popcount(1ul); // calling go-to-definition in clangd will jump to line2.
}

The __builtin_popcount FunctionDecl looks like, its source location points to the first call site (line 2, col 10). The source location is suspicious (I think it should be an invalid source loc).

`-FunctionDecl 0x560139b6dd20 <col:10> col:10 implicit used __builtin_popcount 'int (unsigned int) noexcept' extern
|   |-ParmVarDecl 0x560139b6de20 <<invalid sloc>> <invalid sloc> 'unsigned int'
|   |-BuiltinAttr 0x560139b6ddc8 <<invalid sloc>> Implicit 378
|   |-NoThrowAttr 0x560139b6de90 <col:10> Implicit
|   `-ConstAttr 0x560139b6deb8 <col:10> Implicit
@hokein hokein added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Mar 30, 2023
@llvmbot
Copy link
Member

llvmbot commented Mar 30, 2023

@llvm/issue-subscribers-clang-frontend

@shafik
Copy link
Collaborator

shafik commented Mar 30, 2023

It looks like we do the same thing w/ other builtins, I am not sure if this is expected behavior or not though, CC @AaronBallman

@AaronBallman
Copy link
Collaborator

As best I can tell, this has been the behavior since at least Clang 3.5, so it's a bit tough to say whether it is or isn't expected. Sema::CreateBuiltin() takes a source location for where the implicit declaration lives, but

LazilyCreateBuiltin(II, BuiltinID, TUScope,
is what picks the source location to pass, and it's passing the location of the name.

I'm not certain what will break if we change the logic, but I would not expect this to be an invalid source location, but instead be in the <built-in> location (same as predefined macros).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

4 participants