[cfe-dev] Implementing __builtin_LINE(x) using a clang plugin
John McCall via cfe-dev
cfe-dev at lists.llvm.org
Wed Jan 1 20:52:04 PST 2020
On 22 Dec 2019, at 3:17, Blaise Tine via cfe-dev wrote:
> Hi all,
> I have C++ DSL that defines its own primitive types and operators
> and I want to provide some debugging facilities during compilation to
> display the source location (filename, line number) for uninitialized
> DSL objects.To capture the source line information for a given DSL
> object, I'm using __builtin_LINE(x) inside the class constructor,
> where 'x' is the function nesting depth (see below).
`__builtin_LINE()` does not take an argument in either Clang or GCC, as
far as I can tell, so I’ll assume you’re asking whether that would
be a reasonable extension. And I don’t know why a *nesting* depth
(i.e. the number of functions/structs/whatever that the current source
location is nested within) would affect the result of
`__builtin_LINE()`, so I’ll also assume you actually mean a *stack*
depth (i.e. you would like to return the line number of the caller). If
that’s correct, then the answer is simple: it is not reasonable to
implement such a feature in either a plugin or in the normal compiler.
I can see two ways to implement it: (1) emit a specialized version of
the function for every caller or (2) dynamically look up line-table
information associated with the return address. Both approaches would
be highly complex and have serious drawbacks.
Fortunately, you don’t really need any of that, because the whole
point of `__builtin_LINE()` is to be used in a default argument
expression, where it will be evaluated in the context of the caller:
```
class DSLObject {
public:
DSLObject(unsigned lineno = __builtin_LINE()) : lineno_(lineno) {}
private:
int lineno_;
};
```
John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200101/e9c73c2c/attachment-0001.html>
More information about the cfe-dev
mailing list