blob: 0841791e08638c92e046bd9143d981cf34498232 [file] [log] [blame]
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001//===- unittests/Lex/PPCallbacksTest.cpp - PPCallbacks tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===--------------------------------------------------------------===//
9
Chandler Carruth320d9662012-12-04 09:45:34 +000010#include "clang/Lex/Preprocessor.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000011#include "clang/AST/ASTConsumer.h"
12#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000013#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000014#include "clang/Basic/DiagnosticOptions.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/LangOptions.h"
Duncan P. N. Exon Smith079c40e2017-03-17 22:55:13 +000017#include "clang/Basic/MemoryBufferCache.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/TargetInfo.h"
20#include "clang/Basic/TargetOptions.h"
21#include "clang/Lex/HeaderSearch.h"
22#include "clang/Lex/HeaderSearchOptions.h"
23#include "clang/Lex/ModuleLoader.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000024#include "clang/Lex/PreprocessorOptions.h"
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +000025#include "clang/Parse/Parser.h"
26#include "clang/Sema/Sema.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000027#include "llvm/ADT/SmallString.h"
Rafael Espindola552c1692013-06-11 22:15:02 +000028#include "llvm/Support/Path.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000029#include "gtest/gtest.h"
30
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000031using namespace clang;
32
33namespace {
34
35// Stub out module loading.
36class VoidModuleLoader : public ModuleLoader {
John Thompson2d94bbb2014-04-23 19:04:32 +000037 ModuleLoadResult loadModule(SourceLocation ImportLoc,
38 ModuleIdPath Path,
39 Module::NameVisibilityKind Visibility,
40 bool IsInclusionDirective) override {
Douglas Gregor8c058932012-11-30 00:01:57 +000041 return ModuleLoadResult();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000042 }
NAKAMURA Takumie73d2a92013-01-12 02:16:29 +000043
John Thompson2d94bbb2014-04-23 19:04:32 +000044 void makeModuleVisible(Module *Mod,
45 Module::NameVisibilityKind Visibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +000046 SourceLocation ImportLoc) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000047
John Thompson2d94bbb2014-04-23 19:04:32 +000048 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Craig Topper416fa342014-06-08 08:38:12 +000049 { return nullptr; }
John Thompson2d94bbb2014-04-23 19:04:32 +000050 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Hans Wennborg4afe5042015-07-22 20:46:26 +000051 { return 0; }
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000052};
53
54// Stub to collect data from InclusionDirective callbacks.
55class InclusionDirectiveCallbacks : public PPCallbacks {
56public:
Alexander Kornienko34eb2072015-04-11 02:00:23 +000057 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
58 StringRef FileName, bool IsAngled,
59 CharSourceRange FilenameRange, const FileEntry *File,
60 StringRef SearchPath, StringRef RelativePath,
61 const Module *Imported) override {
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000062 this->HashLoc = HashLoc;
63 this->IncludeTok = IncludeTok;
64 this->FileName = FileName.str();
65 this->IsAngled = IsAngled;
66 this->FilenameRange = FilenameRange;
67 this->File = File;
68 this->SearchPath = SearchPath.str();
69 this->RelativePath = RelativePath.str();
70 this->Imported = Imported;
71 }
72
73 SourceLocation HashLoc;
74 Token IncludeTok;
75 SmallString<16> FileName;
76 bool IsAngled;
77 CharSourceRange FilenameRange;
78 const FileEntry* File;
79 SmallString<16> SearchPath;
80 SmallString<16> RelativePath;
81 const Module* Imported;
82};
83
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +000084// Stub to collect data from PragmaOpenCLExtension callbacks.
85class PragmaOpenCLExtensionCallbacks : public PPCallbacks {
86public:
87 typedef struct {
Alexey Samsonov05747f32013-10-14 07:13:59 +000088 SmallString<16> Name;
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +000089 unsigned State;
90 } CallbackParameters;
91
Hans Wennborg4afe5042015-07-22 20:46:26 +000092 PragmaOpenCLExtensionCallbacks() : Name("Not called."), State(99) {}
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +000093
Alexander Kornienko34eb2072015-04-11 02:00:23 +000094 void PragmaOpenCLExtension(clang::SourceLocation NameLoc,
95 const clang::IdentifierInfo *Name,
96 clang::SourceLocation StateLoc,
97 unsigned State) override {
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +000098 this->NameLoc = NameLoc;
Alexey Samsonov05747f32013-10-14 07:13:59 +000099 this->Name = Name->getName();
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000100 this->StateLoc = StateLoc;
101 this->State = State;
Hans Wennborg4afe5042015-07-22 20:46:26 +0000102 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000103
104 SourceLocation NameLoc;
Alexey Samsonov05747f32013-10-14 07:13:59 +0000105 SmallString<16> Name;
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000106 SourceLocation StateLoc;
107 unsigned State;
108};
109
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000110// PPCallbacks test fixture.
111class PPCallbacksTest : public ::testing::Test {
112protected:
113 PPCallbacksTest()
Benjamin Kramer46d311d2015-10-08 14:20:14 +0000114 : InMemoryFileSystem(new vfs::InMemoryFileSystem),
115 FileMgr(FileSystemOptions(), InMemoryFileSystem),
116 DiagID(new DiagnosticIDs()), DiagOpts(new DiagnosticOptions()),
Alp Toker80758082014-07-06 05:26:44 +0000117 Diags(DiagID, DiagOpts.get(), new IgnoringDiagConsumer()),
118 SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions()) {
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000119 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
Alp Toker80758082014-07-06 05:26:44 +0000120 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000121 }
122
Benjamin Kramer46d311d2015-10-08 14:20:14 +0000123 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem;
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000124 FileManager FileMgr;
125 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
126 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
127 DiagnosticsEngine Diags;
128 SourceManager SourceMgr;
129 LangOptions LangOpts;
Alp Toker80758082014-07-06 05:26:44 +0000130 std::shared_ptr<TargetOptions> TargetOpts;
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000131 IntrusiveRefCntPtr<TargetInfo> Target;
132
133 // Register a header path as a known file and add its location
134 // to search path.
135 void AddFakeHeader(HeaderSearch& HeaderInfo, const char* HeaderPath,
136 bool IsSystemHeader) {
137 // Tell FileMgr about header.
Benjamin Kramer46d311d2015-10-08 14:20:14 +0000138 InMemoryFileSystem->addFile(HeaderPath, 0,
139 llvm::MemoryBuffer::getMemBuffer("\n"));
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000140
141 // Add header's parent path to search path.
Alexey Samsonovd1127d22014-10-15 22:00:40 +0000142 StringRef SearchPath = llvm::sys::path::parent_path(HeaderPath);
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000143 const DirectoryEntry *DE = FileMgr.getDirectory(SearchPath);
Daniel Dunbarae4feb62013-01-25 01:50:28 +0000144 DirectoryLookup DL(DE, SrcMgr::C_User, false);
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000145 HeaderInfo.AddSearchPath(DL, IsSystemHeader);
146 }
147
148 // Get the raw source string of the range.
149 StringRef GetSourceString(CharSourceRange Range) {
150 const char* B = SourceMgr.getCharacterData(Range.getBegin());
151 const char* E = SourceMgr.getCharacterData(Range.getEnd());
152
153 return StringRef(B, E - B);
154 }
155
156 // Run lexer over SourceText and collect FilenameRange from
157 // the InclusionDirective callback.
158 CharSourceRange InclusionDirectiveFilenameRange(const char* SourceText,
159 const char* HeaderPath, bool SystemHeader) {
Alexey Samsonovd1127d22014-10-15 22:00:40 +0000160 std::unique_ptr<llvm::MemoryBuffer> Buf =
161 llvm::MemoryBuffer::getMemBuffer(SourceText);
David Blaikie50a5f972014-08-29 07:59:55 +0000162 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000163
164 VoidModuleLoader ModLoader;
Duncan P. N. Exon Smith079c40e2017-03-17 22:55:13 +0000165 MemoryBufferCache PCMCache;
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000166
David Blaikie9c28cb32017-01-06 01:04:46 +0000167 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
168 Diags, LangOpts, Target.get());
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000169 AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader);
170
David Blaikiee3041682017-01-05 19:11:36 +0000171 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
Duncan P. N. Exon Smith079c40e2017-03-17 22:55:13 +0000172 SourceMgr, PCMCache, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000173 /*IILookup =*/nullptr,
Alp Toker96637802014-05-02 03:43:38 +0000174 /*OwnsHeaderSearch =*/false);
Alp Toker1ae02f62014-05-02 03:43:30 +0000175 PP.Initialize(*Target);
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000176 InclusionDirectiveCallbacks* Callbacks = new InclusionDirectiveCallbacks;
Craig Topperb8a70532014-09-10 04:53:53 +0000177 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000178
179 // Lex source text.
180 PP.EnterMainSourceFile();
181
182 while (true) {
183 Token Tok;
184 PP.Lex(Tok);
185 if (Tok.is(tok::eof))
186 break;
187 }
188
189 // Callbacks have been executed at this point -- return filename range.
190 return Callbacks->FilenameRange;
191 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000192
193 PragmaOpenCLExtensionCallbacks::CallbackParameters
194 PragmaOpenCLExtensionCall(const char* SourceText) {
195 LangOptions OpenCLLangOpts;
196 OpenCLLangOpts.OpenCL = 1;
197
Alexey Samsonovd1127d22014-10-15 22:00:40 +0000198 std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
199 llvm::MemoryBuffer::getMemBuffer(SourceText, "test.cl");
David Blaikie50a5f972014-08-29 07:59:55 +0000200 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000201
202 VoidModuleLoader ModLoader;
Duncan P. N. Exon Smith079c40e2017-03-17 22:55:13 +0000203 MemoryBufferCache PCMCache;
David Blaikie9c28cb32017-01-06 01:04:46 +0000204 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
205 Diags, OpenCLLangOpts, Target.get());
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000206
David Blaikiee3041682017-01-05 19:11:36 +0000207 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags,
Duncan P. N. Exon Smith079c40e2017-03-17 22:55:13 +0000208 OpenCLLangOpts, SourceMgr, PCMCache, HeaderInfo, ModLoader,
David Blaikiee3041682017-01-05 19:11:36 +0000209 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000210 /*OwnsHeaderSearch =*/false);
211 PP.Initialize(*Target);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000212
213 // parser actually sets correct pragma handlers for preprocessor
214 // according to LangOptions, so we init Parser to register opencl
215 // pragma handlers
Alp Toker08043432014-05-03 03:46:04 +0000216 ASTContext Context(OpenCLLangOpts, SourceMgr,
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000217 PP.getIdentifierTable(), PP.getSelectorTable(),
Alp Toker08043432014-05-03 03:46:04 +0000218 PP.getBuiltinInfo());
219 Context.InitBuiltinTypes(*Target);
220
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000221 ASTConsumer Consumer;
222 Sema S(PP, Context, Consumer);
223 Parser P(PP, S, false);
224 PragmaOpenCLExtensionCallbacks* Callbacks = new PragmaOpenCLExtensionCallbacks;
Craig Topperb8a70532014-09-10 04:53:53 +0000225 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000226
227 // Lex source text.
228 PP.EnterMainSourceFile();
229 while (true) {
230 Token Tok;
231 PP.Lex(Tok);
232 if (Tok.is(tok::eof))
233 break;
234 }
235
236 PragmaOpenCLExtensionCallbacks::CallbackParameters RetVal = {
Alexey Samsonov05747f32013-10-14 07:13:59 +0000237 Callbacks->Name,
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000238 Callbacks->State
239 };
240 return RetVal;
241 }
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000242};
243
244TEST_F(PPCallbacksTest, QuotedFilename) {
245 const char* Source =
246 "#include \"quoted.h\"\n";
247
248 CharSourceRange Range =
249 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
250
251 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
252}
253
254TEST_F(PPCallbacksTest, AngledFilename) {
255 const char* Source =
256 "#include <angled.h>\n";
257
258 CharSourceRange Range =
259 InclusionDirectiveFilenameRange(Source, "/angled.h", true);
260
261 ASSERT_EQ("<angled.h>", GetSourceString(Range));
262}
263
264TEST_F(PPCallbacksTest, QuotedInMacro) {
265 const char* Source =
266 "#define MACRO_QUOTED \"quoted.h\"\n"
267 "#include MACRO_QUOTED\n";
268
269 CharSourceRange Range =
270 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
271
272 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
273}
274
275TEST_F(PPCallbacksTest, AngledInMacro) {
276 const char* Source =
277 "#define MACRO_ANGLED <angled.h>\n"
278 "#include MACRO_ANGLED\n";
279
280 CharSourceRange Range =
281 InclusionDirectiveFilenameRange(Source, "/angled.h", true);
282
283 ASSERT_EQ("<angled.h>", GetSourceString(Range));
284}
285
286TEST_F(PPCallbacksTest, StringizedMacroArgument) {
287 const char* Source =
288 "#define MACRO_STRINGIZED(x) #x\n"
289 "#include MACRO_STRINGIZED(quoted.h)\n";
290
291 CharSourceRange Range =
292 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
293
294 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
295}
296
297TEST_F(PPCallbacksTest, ConcatenatedMacroArgument) {
298 const char* Source =
299 "#define MACRO_ANGLED <angled.h>\n"
300 "#define MACRO_CONCAT(x, y) x ## _ ## y\n"
301 "#include MACRO_CONCAT(MACRO, ANGLED)\n";
302
303 CharSourceRange Range =
304 InclusionDirectiveFilenameRange(Source, "/angled.h", false);
305
306 ASSERT_EQ("<angled.h>", GetSourceString(Range));
307}
308
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000309TEST_F(PPCallbacksTest, TrigraphFilename) {
310 const char* Source =
Benjamin Kramerf9db1302012-11-03 20:58:26 +0000311 "#include \"tri\?\?-graph.h\"\n";
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000312
313 CharSourceRange Range =
314 InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
315
Benjamin Kramerf9db1302012-11-03 20:58:26 +0000316 ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000317}
318
319TEST_F(PPCallbacksTest, TrigraphInMacro) {
320 const char* Source =
Benjamin Kramerf9db1302012-11-03 20:58:26 +0000321 "#define MACRO_TRIGRAPH \"tri\?\?-graph.h\"\n"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000322 "#include MACRO_TRIGRAPH\n";
323
324 CharSourceRange Range =
325 InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
326
Benjamin Kramerf9db1302012-11-03 20:58:26 +0000327 ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000328}
329
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000330TEST_F(PPCallbacksTest, OpenCLExtensionPragmaEnabled) {
331 const char* Source =
332 "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
333
334 PragmaOpenCLExtensionCallbacks::CallbackParameters Parameters =
335 PragmaOpenCLExtensionCall(Source);
336
337 ASSERT_EQ("cl_khr_fp64", Parameters.Name);
338 unsigned ExpectedState = 1;
339 ASSERT_EQ(ExpectedState, Parameters.State);
340}
341
342TEST_F(PPCallbacksTest, OpenCLExtensionPragmaDisabled) {
343 const char* Source =
344 "#pragma OPENCL EXTENSION cl_khr_fp16 : disable\n";
345
346 PragmaOpenCLExtensionCallbacks::CallbackParameters Parameters =
347 PragmaOpenCLExtensionCall(Source);
348
349 ASSERT_EQ("cl_khr_fp16", Parameters.Name);
350 unsigned ExpectedState = 0;
351 ASSERT_EQ(ExpectedState, Parameters.State);
352}
353
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000354} // anonoymous namespace