From 2f84230495c48ff3c1374151ce35a6c0a54ad01e Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Wed, 11 Sep 2019 20:40:31 +0000 Subject: [clang-scan-deps] add skip excluded conditional preprocessor block preprocessing optimization This commit adds an optimization to clang-scan-deps and clang's preprocessor that skips excluded preprocessor blocks by bumping the lexer pointer, and not lexing the tokens until reaching appropriate #else/#endif directive. The skip positions and lexer offsets are computed when the file is minimized, directly from the minimized tokens. On an 18-core iMacPro with macOS Catalina Beta I got 10-15% speed-up from this optimization when running clang-scan-deps on the compilation database for a recent LLVM and Clang (3511 files). Differential Revision: https://reviews.llvm.org/D67127 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@371656 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../DependencyDirectivesSourceMinimizerTest.cpp | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'unittests/Lex') diff --git a/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp b/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp index 8770050c86..530519573a 100644 --- a/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp +++ b/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp @@ -617,4 +617,46 @@ ort \ minimize_source_to_dependency_directives::cxx_module_decl); } +TEST(MinimizeSourceToDependencyDirectivesTest, SkippedPPRangesBasic) { + SmallString<128> Out; + SmallVector Toks; + StringRef Source = "#ifndef GUARD\n" + "#define GUARD\n" + "void foo();\n" + "#endif\n"; + ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Toks)); + SmallVector Ranges; + ASSERT_FALSE(computeSkippedRanges(Toks, Ranges)); + EXPECT_EQ(Ranges.size(), 1u); + EXPECT_EQ(Ranges[0].Offset, 0); + EXPECT_EQ(Ranges[0].Length, (int)Out.find("#endif")); +} + +TEST(MinimizeSourceToDependencyDirectivesTest, SkippedPPRangesNested) { + SmallString<128> Out; + SmallVector Toks; + StringRef Source = "#ifndef GUARD\n" + "#define GUARD\n" + "#if FOO\n" + "#include hello\n" + "#elif BAR\n" + "#include bye\n" + "#endif\n" + "#else\n" + "#include nothing\n" + "#endif\n"; + ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Toks)); + SmallVector Ranges; + ASSERT_FALSE(computeSkippedRanges(Toks, Ranges)); + EXPECT_EQ(Ranges.size(), 4u); + EXPECT_EQ(Ranges[0].Offset, (int)Out.find("#if FOO")); + EXPECT_EQ(Ranges[0].Offset + Ranges[0].Length, (int)Out.find("#elif")); + EXPECT_EQ(Ranges[1].Offset, (int)Out.find("#elif BAR")); + EXPECT_EQ(Ranges[1].Offset + Ranges[1].Length, (int)Out.find("#endif")); + EXPECT_EQ(Ranges[2].Offset, 0); + EXPECT_EQ(Ranges[2].Length, (int)Out.find("#else")); + EXPECT_EQ(Ranges[3].Offset, (int)Out.find("#else")); + EXPECT_EQ(Ranges[3].Offset + Ranges[3].Length, (int)Out.rfind("#endif")); +} + } // end anonymous namespace -- cgit v1.2.1