blob: f020206470acc4ae7269ab4373150f1e9f4946d5 [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"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/TargetInfo.h"
19#include "clang/Basic/TargetOptions.h"
20#include "clang/Lex/HeaderSearch.h"
21#include "clang/Lex/HeaderSearchOptions.h"
22#include "clang/Lex/ModuleLoader.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000023#include "clang/Lex/PreprocessorOptions.h"
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +000024#include "clang/Parse/Parser.h"
25#include "clang/Sema/Sema.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000026#include "llvm/ADT/SmallString.h"
Rafael Espindola552c1692013-06-11 22:15:02 +000027#include "llvm/Support/Path.h"
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000028#include "gtest/gtest.h"
29
30using namespace llvm;
31using namespace llvm::sys;
32using namespace clang;
33
34namespace {
35
36// Stub out module loading.
37class VoidModuleLoader : public ModuleLoader {
John Thompson2d94bbb2014-04-23 19:04:32 +000038 ModuleLoadResult loadModule(SourceLocation ImportLoc,
39 ModuleIdPath Path,
40 Module::NameVisibilityKind Visibility,
41 bool IsInclusionDirective) override {
Douglas Gregor8c058932012-11-30 00:01:57 +000042 return ModuleLoadResult();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000043 }
NAKAMURA Takumie73d2a92013-01-12 02:16:29 +000044
John Thompson2d94bbb2014-04-23 19:04:32 +000045 void makeModuleVisible(Module *Mod,
46 Module::NameVisibilityKind Visibility,
47 SourceLocation ImportLoc,
48 bool Complain) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000049
John Thompson2d94bbb2014-04-23 19:04:32 +000050 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Craig Topper416fa342014-06-08 08:38:12 +000051 { return nullptr; }
John Thompson2d94bbb2014-04-23 19:04:32 +000052 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
53 { return 0; };
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +000054};
55
56// Stub to collect data from InclusionDirective callbacks.
57class InclusionDirectiveCallbacks : public PPCallbacks {
58public:
59 void InclusionDirective(SourceLocation HashLoc,
60 const Token &IncludeTok,
61 StringRef FileName,
62 bool IsAngled,
63 CharSourceRange FilenameRange,
64 const FileEntry *File,
65 StringRef SearchPath,
66 StringRef RelativePath,
67 const Module *Imported) {
68 this->HashLoc = HashLoc;
69 this->IncludeTok = IncludeTok;
70 this->FileName = FileName.str();
71 this->IsAngled = IsAngled;
72 this->FilenameRange = FilenameRange;
73 this->File = File;
74 this->SearchPath = SearchPath.str();
75 this->RelativePath = RelativePath.str();
76 this->Imported = Imported;
77 }
78
79 SourceLocation HashLoc;
80 Token IncludeTok;
81 SmallString<16> FileName;
82 bool IsAngled;
83 CharSourceRange FilenameRange;
84 const FileEntry* File;
85 SmallString<16> SearchPath;
86 SmallString<16> RelativePath;
87 const Module* Imported;
88};
89
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +000090// Stub to collect data from PragmaOpenCLExtension callbacks.
91class PragmaOpenCLExtensionCallbacks : public PPCallbacks {
92public:
93 typedef struct {
Alexey Samsonov05747f32013-10-14 07:13:59 +000094 SmallString<16> Name;
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +000095 unsigned State;
96 } CallbackParameters;
97
98 PragmaOpenCLExtensionCallbacks() : Name("Not called."), State(99) {};
99
100 void PragmaOpenCLExtension(
101 clang::SourceLocation NameLoc, const clang::IdentifierInfo *Name,
102 clang::SourceLocation StateLoc, unsigned State) {
103 this->NameLoc = NameLoc;
Alexey Samsonov05747f32013-10-14 07:13:59 +0000104 this->Name = Name->getName();
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000105 this->StateLoc = StateLoc;
106 this->State = State;
107 };
108
109 SourceLocation NameLoc;
Alexey Samsonov05747f32013-10-14 07:13:59 +0000110 SmallString<16> Name;
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000111 SourceLocation StateLoc;
112 unsigned State;
113};
114
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000115// PPCallbacks test fixture.
116class PPCallbacksTest : public ::testing::Test {
117protected:
118 PPCallbacksTest()
Alp Toker80758082014-07-06 05:26:44 +0000119 : FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()),
120 DiagOpts(new DiagnosticOptions()),
121 Diags(DiagID, DiagOpts.get(), new IgnoringDiagConsumer()),
122 SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions()) {
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000123 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
Alp Toker80758082014-07-06 05:26:44 +0000124 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000125 }
126
127 FileSystemOptions FileMgrOpts;
128 FileManager FileMgr;
129 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
130 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
131 DiagnosticsEngine Diags;
132 SourceManager SourceMgr;
133 LangOptions LangOpts;
Alp Toker80758082014-07-06 05:26:44 +0000134 std::shared_ptr<TargetOptions> TargetOpts;
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000135 IntrusiveRefCntPtr<TargetInfo> Target;
136
137 // Register a header path as a known file and add its location
138 // to search path.
139 void AddFakeHeader(HeaderSearch& HeaderInfo, const char* HeaderPath,
140 bool IsSystemHeader) {
141 // Tell FileMgr about header.
142 FileMgr.getVirtualFile(HeaderPath, 0, 0);
143
144 // Add header's parent path to search path.
145 StringRef SearchPath = path::parent_path(HeaderPath);
146 const DirectoryEntry *DE = FileMgr.getDirectory(SearchPath);
Daniel Dunbarae4feb62013-01-25 01:50:28 +0000147 DirectoryLookup DL(DE, SrcMgr::C_User, false);
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000148 HeaderInfo.AddSearchPath(DL, IsSystemHeader);
149 }
150
151 // Get the raw source string of the range.
152 StringRef GetSourceString(CharSourceRange Range) {
153 const char* B = SourceMgr.getCharacterData(Range.getBegin());
154 const char* E = SourceMgr.getCharacterData(Range.getEnd());
155
156 return StringRef(B, E - B);
157 }
158
159 // Run lexer over SourceText and collect FilenameRange from
160 // the InclusionDirective callback.
161 CharSourceRange InclusionDirectiveFilenameRange(const char* SourceText,
162 const char* HeaderPath, bool SystemHeader) {
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000163 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(SourceText);
David Blaikie50a5f972014-08-29 07:59:55 +0000164 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000165
166 VoidModuleLoader ModLoader;
167
168 IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts = new HeaderSearchOptions();
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000169 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts,
Alp Tokerf994cef2014-07-05 03:08:06 +0000170 Target.get());
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000171 AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader);
172
173 IntrusiveRefCntPtr<PreprocessorOptions> PPOpts = new PreprocessorOptions();
Alp Toker96637802014-05-02 03:43:38 +0000174 Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000175 /*IILookup =*/nullptr,
Alp Toker96637802014-05-02 03:43:38 +0000176 /*OwnsHeaderSearch =*/false);
Alp Toker1ae02f62014-05-02 03:43:30 +0000177 PP.Initialize(*Target);
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000178 InclusionDirectiveCallbacks* Callbacks = new InclusionDirectiveCallbacks;
179 PP.addPPCallbacks(Callbacks); // Takes ownership.
180
181 // Lex source text.
182 PP.EnterMainSourceFile();
183
184 while (true) {
185 Token Tok;
186 PP.Lex(Tok);
187 if (Tok.is(tok::eof))
188 break;
189 }
190
191 // Callbacks have been executed at this point -- return filename range.
192 return Callbacks->FilenameRange;
193 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000194
195 PragmaOpenCLExtensionCallbacks::CallbackParameters
196 PragmaOpenCLExtensionCall(const char* SourceText) {
197 LangOptions OpenCLLangOpts;
198 OpenCLLangOpts.OpenCL = 1;
199
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000200 std::unique_ptr<MemoryBuffer> SourceBuf =
201 MemoryBuffer::getMemBuffer(SourceText, "test.cl");
David Blaikie50a5f972014-08-29 07:59:55 +0000202 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000203
204 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000205 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags,
Alp Tokerf994cef2014-07-05 03:08:06 +0000206 OpenCLLangOpts, Target.get());
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +0000207
Alp Toker96637802014-05-02 03:43:38 +0000208 Preprocessor PP(new PreprocessorOptions(), Diags, OpenCLLangOpts, SourceMgr,
Craig Topper416fa342014-06-08 08:38:12 +0000209 HeaderInfo, ModLoader, /*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;
225 PP.addPPCallbacks(Callbacks); // Takes ownership.
226
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
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +0000354} // anonoymous namespace