[cfe-dev] Decls are not synonyms for the symbols they represent

Ted Kremenek kremenek at apple.com
Wed Sep 17 12:52:32 PDT 2008


On Sep 17, 2008, at 12:19 PM, steve naroff wrote:

>>>
>>> extern int x;
>>>
>>> void foo() {
>>> x++;
>>> }
>>>
>>> extern int x;
>>>
>>> void bar() {
>>> x++;
>>> }
>>>
>>> void baz() {
>>>  extern int x;
>>>  x++;
>>> }
>>>
>>> -----
>>>
>>> If you do an AST dump of this, you will see that the DeclRefExprs  
>>> for 'x' in foo(), bar(), and baz() respectively refer to separate  
>>> VarDecls.  Right now a client of the ASTs has no way of knowing  
>>> that these VarDecls refer to the same variable.  Certainly a  
>>> higher-level API that resolves identifiers across translation  
>>> units could do this, but it might have to do a lot of work to  
>>> resolve situations like the one above.  By having a mechanism to  
>>> resolve identifiers within a translation unit, this higher-level  
>>> API would much easier to implement.
>>
>> An "IdResolver" used beyond Sema would be very useful, but it may  
>> come with a memory cost.
>> Could we get away with having all DeclRefExprs for 'x' refer to the  
>> same VarDecl ?
>>
>
> I guess we could, however I'm still not sure it's the right thing to  
> do (since it goes against our tenet of keeping the AST simple and  
> reflective of the source). From the compiler's perspective, the 3  
> DeclRefExprs *should* bind to 3 different VarDecls.
>
> For this example, I'd like to know why/when we care if all these  
> VarDecls refer to the same variable? Since each VarDecl is "extern",  
> it's the linkers job to bind the actual variable definition (which  
> isn't in this particular translation unit).

Another example would be a client that wanted to rename 'x'.  How  
would it know all of the declarations for the same variable?  There  
are lots of cases where clients, looking the AST for a translation  
unit, want to know which declarations refer to the same thing.



More information about the cfe-dev mailing list