Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
From f612539aaee3a46abbbc8fa30d0263d9b49dca86 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johann=20Kl=C3=A4hn?= <dev@jklaehn.de>
Date: Mon, 10 Jul 2017 12:34:10 +0200
Subject: [PATCH 08/12] Fix printing policy for AST context loaded from file
In ASTUnit::LoadFromASTFile, the context object is set up using default-constructed
LangOptions (which only later get populated). As the language options are used in the constructor
of PrintingPolicy, this needs to be updated explicitly after the language options are available.
---
lib/Frontend/ASTUnit.cpp | 3 ++
unittests/Frontend/ASTUnitTest.cpp | 87 ++++++++++++++++++++++++++++++++++++++
unittests/Frontend/CMakeLists.txt | 1 +
3 files changed, 91 insertions(+)
create mode 100644 unittests/Frontend/ASTUnitTest.cpp
diff --git a/tools/clang/lib/Frontend/ASTUnit.cpp b/tools/clang/lib/Frontend/ASTUnit.cpp
index 875f21d69a..bb1ea6c4f8 100644
--- a/tools/clang/lib/Frontend/ASTUnit.cpp
+++ b/tools/clang/lib/Frontend/ASTUnit.cpp
@@ -546,6 +546,9 @@ private:
// Initialize the ASTContext
Context->InitBuiltinTypes(*Target);
+ // Adjust printing policy based on language options.
+ Context->setPrintingPolicy(PrintingPolicy(LangOpt));
+
// We didn't have access to the comment options when the ASTContext was
// constructed, so register them now.
Context->getCommentCommandTraits().registerCommentOptions(
diff --git a/tools/clang/unittests/Frontend/ASTUnitTest.cpp b/tools/clang/unittests/Frontend/ASTUnitTest.cpp
new file mode 100644
index 0000000000..a7d08a992f
--- /dev/null
+++ b/tools/clang/unittests/Frontend/ASTUnitTest.cpp
@@ -0,0 +1,87 @@
+//===- unittests/Frontend/ASTUnitTest.cpp - ASTUnit tests -----------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <fstream>
+
+#include "clang/Frontend/ASTUnit.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/CompilerInvocation.h"
+#include "clang/Frontend/PCHContainerOperations.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace clang;
+
+namespace {
+
+TEST(ASTUnit, SaveLoadPreservesLangOptionsInPrintingPolicy) {
+ // Check that the printing policy is restored with the correct language
+ // options when loading an ASTUnit from a file. To this end, an ASTUnit
+ // for a C++ translation unit is set up and written to a temporary file.
+
+ // By default `UseVoidForZeroParams` is true for non-C++ language options,
+ // thus we can check this field after loading the ASTUnit to deduce whether
+ // the correct (C++) language options were used when setting up the printing
+ // policy.
+
+ {
+ PrintingPolicy PolicyWithDefaultLangOpt(LangOptions{});
+ EXPECT_TRUE(PolicyWithDefaultLangOpt.UseVoidForZeroParams);
+ }
+
+ int FD;
+ llvm::SmallString<256> InputFileName;
+ ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "cpp", FD, InputFileName));
+ tool_output_file input_file(InputFileName, FD);
+ input_file.os() << "";
+
+ const char* Args[] = {"clang", "-xc++", InputFileName.c_str()};
+
+ IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
+ CompilerInstance::createDiagnostics(new DiagnosticOptions());
+
+ std::shared_ptr<CompilerInvocation> CInvok =
+ createInvocationFromCommandLine(Args, Diags);
+
+ if (!CInvok)
+ FAIL() << "could not create compiler invocation";
+
+ FileManager *FileMgr =
+ new FileManager(FileSystemOptions(), vfs::getRealFileSystem());
+ auto PCHContainerOps = std::make_shared<PCHContainerOperations>();
+
+ std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
+ CInvok, PCHContainerOps, Diags, FileMgr);
+
+ if (!AST)
+ FAIL() << "failed to create ASTUnit";
+
+ EXPECT_FALSE(AST->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
+
+ llvm::SmallString<256> ASTFileName;
+ ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "ast", FD, ASTFileName));
+ tool_output_file ast_file(ASTFileName, FD);
+ AST->Save(ASTFileName.str());
+
+ EXPECT_TRUE(llvm::sys::fs::exists(ASTFileName));
+
+ std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
+ ASTFileName.str(), PCHContainerOps->getRawReader(), ASTUnit::LoadEverything, Diags,
+ FileSystemOptions(), /*UseDebugInfo=*/false);
+
+ if (!AU)
+ FAIL() << "failed to load ASTUnit";
+
+ EXPECT_FALSE(AU->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
+}
+
+} // anonymous namespace
diff --git a/tools/clang/unittests/Frontend/CMakeLists.txt b/tools/clang/unittests/Frontend/CMakeLists.txt
index 674f77bd01..4312151c04 100644
--- a/tools/clang/unittests/Frontend/CMakeLists.txt
+++ b/tools/clang/unittests/Frontend/CMakeLists.txt
@@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS
)
add_clang_unittest(FrontendTests
+ ASTUnitTest.cpp
FrontendActionTest.cpp
CodeGenActionTest.cpp
)
--
2.13.0