SourceLocation of FunctionDecl

Hi,

I want to add code just before a certain function deceleration.

I do this by visiting the FunctionDecl and using it’s start location for InsertTextBefore.

The problem is that GetLocStart returns the location of the Function’s name and not the entire prototype i.e. if i have this function:

static int f() {
}

i will get the location of “f” and not the location of “static”.

Help?

Thanks.

Can't you do this by adding code *after* the previous declaration entity?

- Fariborz

Actually i tried it but this approach has 2 problems:

  1. I can’t always be sure that there will be a previous declaration.
  2. When i try to receive the end location of the previous entity, i get a location which is 2 or 3 offsets prior to the actual location. I think this is a bug in GetLocEnd.

Long term we need to add location for storage class in each declaration.
Missing that, the next best thing would be to rewrite the declaration into:

static int f();
int f() {}
I checked with Doug and he says we can get location of the type specifier.

  • Fariborz

Yes that would be nice to have.

But i think the greater issue here is the correctness of the location retrieval functions. GetLocStart for a FunctionDecl should retrieve the start of the decleration, including storage class, etc. Overall, many times I’ve seen these function miss the target and that’s a shame.

Then please file bugs or (better yet) submit patches.

It's not always clear-cut what these functions should do. For example, consider:

  int f(), g();

Where is the start of f() and the start of g()? They probably shouldn't overlap, but that means either leaving the decl-specifier-seq out of both of them or only placing the decl-specifier-seq only on f(), both of which are weird.

  - Doug

I see your point. I’ll try and have a go at solving the SourceLocation problem. Could you point me to the relevant code parts? (i’m assuming the problem is in the AST creation part and not specifically in the getLocation functions)

p.s. another problem:

When asking for the qualifiers of:

const int f();

I get null. Am i doing something wrong?

Thanks.

Hi!

Nimrod Partush meinte am 03.11.2010 10:24:

When asking for the qualifiers of:

const int f();

I get null. Am i doing something wrong?

Well, you don't say how you do it... :wink:

Probably you're making the same mistake I did some time ago. What you
want is not the qualifiers of the function (it has none) but those of
its return type.

Regards, Jan.

Nimrod Partush meinte am 02.11.2010 17:20:

2. When i try to receive the end location of the previous entity, i get
a location which is 2 or 3 offsets prior to the actual location. I think
this is a bug in GetLocEnd.

No bug I guess. 'getLocEnd()' uses 'SourceRange().getEnd()' and
'SourceRange' is a bit counterintuitive.

http://clang.llvm.org/docs/InternalsManual.html#SourceRange

Regards, Jan.

Hi,

Well, i tried various ways: Getting the FuncDecl qualifiers, Getting the ResultType qualifiers, Getting the TypeLoc and through that…

Why don’t you tell me how you did it? :slight_smile:

Nimrod Partush meinte am 05.11.2010 16:19:

When asking for the qualifiers of:

const int f();

I get null. Am i doing something wrong?

Well, you don't say how you do it... :wink:

Probably you're making the same mistake I did some time ago. What you
want is not the qualifiers of the function (it has none) but those of
its return type.

Well, i tried various ways: Getting the FuncDecl qualifiers, Getting the
ResultType qualifiers, Getting the TypeLoc and through that...

Why don't you tell me how you did it? :slight_smile:

I'm not sure if we actually do the same thing. I need to know if the
function returns something that is qualified and use

FunctionDecl* fD;
...
fD->getType().isConstQualified()
fD->getType().isVolatileQualified()

But you wrote something about getting NULL which can't happen here. That
is why I was wondering how you try to do it. You could also have a look
at the old thread on this list [1]. Maybe you can use some of the advice
I got.

[1] http://lists.cs.uiuc.edu/pipermail/cfe-dev/2010-September/010923.html

Regards, Jan.

These methods might work fine, but what i need is the location of the qualifier. Any idea how do i translate fD->getType() to a SourceLocation?

Your help is greatly appreciated :slight_smile:

These methods might work fine, but what i need is the location of the qualifier. Any idea how do i translate fD->getType() to a SourceLocation?

Clang doesn’t keep track of the locations of qualifiers. This could be added to QualifiedTypeLoc, but isn’t there now.

  • Doug