1 | //===- unittests/Lex/PPMemoryAllocationsTest.cpp - ----------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===--------------------------------------------------------------===// |
8 | |
9 | #include "clang/Basic/Diagnostic.h" |
10 | #include "clang/Basic/DiagnosticOptions.h" |
11 | #include "clang/Basic/FileManager.h" |
12 | #include "clang/Basic/LangOptions.h" |
13 | #include "clang/Basic/SourceManager.h" |
14 | #include "clang/Basic/TargetInfo.h" |
15 | #include "clang/Basic/TargetOptions.h" |
16 | #include "clang/Lex/HeaderSearch.h" |
17 | #include "clang/Lex/HeaderSearchOptions.h" |
18 | #include "clang/Lex/ModuleLoader.h" |
19 | #include "clang/Lex/Preprocessor.h" |
20 | #include "clang/Lex/PreprocessorOptions.h" |
21 | #include "gtest/gtest.h" |
22 | |
23 | using namespace clang; |
24 | |
25 | namespace { |
26 | |
27 | class PPMemoryAllocationsTest : public ::testing::Test { |
28 | protected: |
29 | PPMemoryAllocationsTest() |
30 | : FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()), |
31 | Diags(DiagID, DiagOpts, new IgnoringDiagConsumer()), |
32 | SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) { |
33 | TargetOpts->Triple = "x86_64-apple-darwin11.1.0" ; |
34 | Target = TargetInfo::CreateTargetInfo(Diags, Opts&: *TargetOpts); |
35 | } |
36 | |
37 | FileSystemOptions FileMgrOpts; |
38 | FileManager FileMgr; |
39 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID; |
40 | DiagnosticOptions DiagOpts; |
41 | DiagnosticsEngine Diags; |
42 | SourceManager SourceMgr; |
43 | LangOptions LangOpts; |
44 | std::shared_ptr<TargetOptions> TargetOpts; |
45 | IntrusiveRefCntPtr<TargetInfo> Target; |
46 | }; |
47 | |
48 | TEST_F(PPMemoryAllocationsTest, PPMacroDefinesAllocations) { |
49 | std::string Source; |
50 | size_t NumMacros = 1000000; |
51 | { |
52 | llvm::raw_string_ostream SourceOS(Source); |
53 | |
54 | // Create a combination of 1 or 3 token macros. |
55 | for (size_t I = 0; I < NumMacros; ++I) { |
56 | SourceOS << "#define MACRO_ID_" << I << " " ; |
57 | if ((I % 2) == 0) |
58 | SourceOS << "(" << I << ")" ; |
59 | else |
60 | SourceOS << I; |
61 | SourceOS << "\n" ; |
62 | } |
63 | } |
64 | |
65 | std::unique_ptr<llvm::MemoryBuffer> Buf = |
66 | llvm::MemoryBuffer::getMemBuffer(InputData: Source); |
67 | SourceMgr.setMainFileID(SourceMgr.createFileID(Buffer: std::move(Buf))); |
68 | |
69 | HeaderSearchOptions HSOpts; |
70 | TrivialModuleLoader ModLoader; |
71 | HeaderSearch (HSOpts, SourceMgr, Diags, LangOpts, Target.get()); |
72 | PreprocessorOptions PPOpts; |
73 | Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, |
74 | /*IILookup =*/nullptr, |
75 | /*OwnsHeaderSearch =*/false); |
76 | PP.Initialize(Target: *Target); |
77 | PP.EnterMainSourceFile(); |
78 | |
79 | PP.LexTokensUntilEOF(); |
80 | |
81 | size_t NumAllocated = PP.getPreprocessorAllocator().getBytesAllocated(); |
82 | float BytesPerDefine = float(NumAllocated) / float(NumMacros); |
83 | llvm::errs() << "Num preprocessor allocations for " << NumMacros |
84 | << " #define: " << NumAllocated << "\n" ; |
85 | llvm::errs() << "Bytes per #define: " << BytesPerDefine << "\n" ; |
86 | // On arm64-apple-macos, we get around 120 bytes per define. |
87 | // Assume a reasonable upper bound based on that number that we don't want |
88 | // to exceed when storing information about a macro #define with 1 or 3 |
89 | // tokens. |
90 | EXPECT_LT(BytesPerDefine, 130.0f); |
91 | } |
92 | |
93 | } // anonymous namespace |
94 | |