xoofx / CppAst.NET

CppAst is a .NET library providing a C/C++ parser for header files powered by Clang/libclang with access to the full AST, comments and macros
BSD 2-Clause "Simplified" License
501 stars 66 forks source link

add ParseAttributes option #20

Closed mugisoba closed 4 years ago

mugisoba commented 4 years ago

This PR adds the ParseAttributes option to skip parsing attributes.

ParseFunctionAttributes and ParseAttributes function takes a long time to complete, so I propose to add an option to skip these functions.

wackoisgod commented 4 years ago

Hmm I shall take a look into the perf, do you feel this has gotten worse than it was before ? or just always has been slow ?

I am also curious how much code are you throwing at it? Is this a larger codebase ?

mugisoba commented 4 years ago

v0.7.0 is slower than v0.6.0.

I tested with this header file. https://github.com/google/flatbuffers/blob/master/include/flatbuffers/flatbuffers.h

var sw = new System.Diagnostics.Stopwatch();
sw.Start();

for (var i = 0; i < 10; ++i)
{
    var options = new CppAst.CppParserOptions().ConfigureForWindowsMsvc(CppAst.CppTargetCpu.X86_64, CppAst.CppVisualStudioVersion.VS2019);
    options.Defines.Add("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH");
    options.IncludeFolders.Add(@"flatbuffers\include");

    var compilation = CppAst.CppParser.ParseFile(
        cppFilename: @"flatbuffers\include\flatbuffers\flatbuffers.h",
        options:options);
}

sw.Stop();
Console.WriteLine(sw.Elapsed);
wackoisgod commented 4 years ago

Yikes! I think I know what is causing this behavior! I do think your changes are good anyway and If you could can you add a test:

public void TestCppNoParseOptionsAttributes()
{
    ParseAssert(@"
[[noreturn]] void x() {};", compilation =>
        {
            Assert.False(compilation.HasErrors);

            Assert.AreEqual(1, compilation.Functions.Count);
            Assert.AreEqual(0, compilation.Functions[0].Attributes.Count);
        },
        // we are using a C++14 attribute because it can be used everywhere
        new CppParserOptions() { AdditionalArguments = { "-std=c++14" }, ParseAttributeEnabled = false }
  );
}
xoofx commented 4 years ago

Actually, I would prefer if ParseAttributeEnabled is set to false by default. We can document it in the readme - as it is done for macros.

mugisoba commented 4 years ago

I fixed it. Please review it again.