0

I have a project that must be parsed before original compilation. It's needed for reflection purposes. In short I want to check edited .h files for some attributes in the code and gather info about it to generate specific include files. For example reflectable classes/fields/methods will be marked as META(parameters...). This macro will be defined like this #define META(...) __attribute__(annotate("reflectable")).

The code should looks like this (similar to Qt QObject or Unreal Engine 4):

1.

// --- macros outside of header to parse ---

// Marks declaration as reflectable (with some metadata), empty for base compiler, useful for clang
#ifdef __clang__
    #define META(...) __attribute__(annotate("reflectable"))
#else
    #define META(...)
#endif

// Injects some generated code after parsing step, empty for clang, useful for base compiler
#ifdef __clang__
    #define GENERATED_REFLECTION_INFO
#else
    #define GENERATED_REFLECTION_INFO GENERATE_CODE(__FILE__, __LINE__)
#endif
// --- reflectable class ---

META(Serializable, Exposed, etc)
class MyReflectableClass : public BaseReflectableClass
{
    GENERATED_REFLECTION_INFO
    
public:
    // Fields examples
    
    META()
    int32 MyReflectableField;

    META(SkipSerialize)
    float MySecondaryReflectableField;

    // Methods example

    META(RemoteMethod)
    void MyReflectableMethod(int32 Param1, uint8 Param2);
};

libclang was used for this goal. But when project has includes like <string> or <memory>, it causes long time parsing of long chain of dependencies in standard library.

Also if project has a lot of headers, similar situation would be happened.

How can I avoid parsing of full standard library? Probably some kind of caches that I could use in libclang?

Or probably are there some ways to skip analyzing of these includes and convince the parser that unknown types are acceptable?

So how can I optimize application to reduce the time of parsing?

Artem Selivanov
  • 1,867
  • 1
  • 27
  • 45
  • 1
    I think this requires more information about the extent of the pre-processing you require to answer accurately and helpfully. – bitmask Aug 02 '20 at 13:23
  • @bitmask what do you mean by "extent of the pre-processing"? I added some info about purpose of usage with code example. – Artem Selivanov Aug 02 '20 at 14:33

1 Answers1

3

C++, as a language, is not very amenable to speculative parsing due to type-dependent parsing. For example, the < token might have different meanings depending upon whether it is preceded by a template or not.

However, libclang supports precompiled headers, which let you cache the standard library headers.

danielschemmel
  • 10,885
  • 1
  • 36
  • 58