1 //===- unittests/Lex/PPCallbacksTest.cpp - PPCallbacks tests ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===--------------------------------------------------------------===//
10 #include "clang/Lex/Preprocessor.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Basic/DiagnosticOptions.h"
15 #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"
23 #include "clang/Lex/PreprocessorOptions.h"
24 #include "clang/Parse/Parser.h"
25 #include "clang/Sema/Sema.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/Path.h"
28 #include "gtest/gtest.h"
30 using namespace clang;
34 // Stub out module loading.
35 class VoidModuleLoader : public ModuleLoader {
36 ModuleLoadResult loadModule(SourceLocation ImportLoc,
38 Module::NameVisibilityKind Visibility,
39 bool IsInclusionDirective) override {
40 return ModuleLoadResult();
43 void makeModuleVisible(Module *Mod,
44 Module::NameVisibilityKind Visibility,
45 SourceLocation ImportLoc) override { }
47 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
49 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
53 // Stub to collect data from InclusionDirective callbacks.
54 class InclusionDirectiveCallbacks : public PPCallbacks {
56 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
57 StringRef FileName, bool IsAngled,
58 CharSourceRange FilenameRange, const FileEntry *File,
59 StringRef SearchPath, StringRef RelativePath,
60 const Module *Imported) override {
61 this->HashLoc = HashLoc;
62 this->IncludeTok = IncludeTok;
63 this->FileName = FileName.str();
64 this->IsAngled = IsAngled;
65 this->FilenameRange = FilenameRange;
67 this->SearchPath = SearchPath.str();
68 this->RelativePath = RelativePath.str();
69 this->Imported = Imported;
72 SourceLocation HashLoc;
74 SmallString<16> FileName;
76 CharSourceRange FilenameRange;
77 const FileEntry* File;
78 SmallString<16> SearchPath;
79 SmallString<16> RelativePath;
80 const Module* Imported;
83 // Stub to collect data from PragmaOpenCLExtension callbacks.
84 class PragmaOpenCLExtensionCallbacks : public PPCallbacks {
91 PragmaOpenCLExtensionCallbacks() : Name("Not called."), State(99) {}
93 void PragmaOpenCLExtension(clang::SourceLocation NameLoc,
94 const clang::IdentifierInfo *Name,
95 clang::SourceLocation StateLoc,
96 unsigned State) override {
97 this->NameLoc = NameLoc;
98 this->Name = Name->getName();
99 this->StateLoc = StateLoc;
103 SourceLocation NameLoc;
104 SmallString<16> Name;
105 SourceLocation StateLoc;
109 // PPCallbacks test fixture.
110 class PPCallbacksTest : public ::testing::Test {
113 : InMemoryFileSystem(new vfs::InMemoryFileSystem),
114 FileMgr(FileSystemOptions(), InMemoryFileSystem),
115 DiagID(new DiagnosticIDs()), DiagOpts(new DiagnosticOptions()),
116 Diags(DiagID, DiagOpts.get(), new IgnoringDiagConsumer()),
117 SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions()) {
118 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
119 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
122 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem;
124 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
125 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
126 DiagnosticsEngine Diags;
127 SourceManager SourceMgr;
128 LangOptions LangOpts;
129 std::shared_ptr<TargetOptions> TargetOpts;
130 IntrusiveRefCntPtr<TargetInfo> Target;
132 // Register a header path as a known file and add its location
134 void AddFakeHeader(HeaderSearch& HeaderInfo, const char* HeaderPath,
135 bool IsSystemHeader) {
136 // Tell FileMgr about header.
137 InMemoryFileSystem->addFile(HeaderPath, 0,
138 llvm::MemoryBuffer::getMemBuffer("\n"));
140 // Add header's parent path to search path.
141 StringRef SearchPath = llvm::sys::path::parent_path(HeaderPath);
142 const DirectoryEntry *DE = FileMgr.getDirectory(SearchPath);
143 DirectoryLookup DL(DE, SrcMgr::C_User, false);
144 HeaderInfo.AddSearchPath(DL, IsSystemHeader);
147 // Get the raw source string of the range.
148 StringRef GetSourceString(CharSourceRange Range) {
149 const char* B = SourceMgr.getCharacterData(Range.getBegin());
150 const char* E = SourceMgr.getCharacterData(Range.getEnd());
152 return StringRef(B, E - B);
155 // Run lexer over SourceText and collect FilenameRange from
156 // the InclusionDirective callback.
157 CharSourceRange InclusionDirectiveFilenameRange(const char* SourceText,
158 const char* HeaderPath, bool SystemHeader) {
159 std::unique_ptr<llvm::MemoryBuffer> Buf =
160 llvm::MemoryBuffer::getMemBuffer(SourceText);
161 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
163 VoidModuleLoader ModLoader;
165 IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts = new HeaderSearchOptions();
166 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts,
168 AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader);
170 IntrusiveRefCntPtr<PreprocessorOptions> PPOpts = new PreprocessorOptions();
171 Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,
172 /*IILookup =*/nullptr,
173 /*OwnsHeaderSearch =*/false);
174 PP.Initialize(*Target);
175 InclusionDirectiveCallbacks* Callbacks = new InclusionDirectiveCallbacks;
176 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
179 PP.EnterMainSourceFile();
184 if (Tok.is(tok::eof))
188 // Callbacks have been executed at this point -- return filename range.
189 return Callbacks->FilenameRange;
192 PragmaOpenCLExtensionCallbacks::CallbackParameters
193 PragmaOpenCLExtensionCall(const char* SourceText) {
194 LangOptions OpenCLLangOpts;
195 OpenCLLangOpts.OpenCL = 1;
197 std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
198 llvm::MemoryBuffer::getMemBuffer(SourceText, "test.cl");
199 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
201 VoidModuleLoader ModLoader;
202 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags,
203 OpenCLLangOpts, Target.get());
205 Preprocessor PP(new PreprocessorOptions(), Diags, OpenCLLangOpts, SourceMgr,
206 HeaderInfo, ModLoader, /*IILookup =*/nullptr,
207 /*OwnsHeaderSearch =*/false);
208 PP.Initialize(*Target);
210 // parser actually sets correct pragma handlers for preprocessor
211 // according to LangOptions, so we init Parser to register opencl
213 ASTContext Context(OpenCLLangOpts, SourceMgr,
214 PP.getIdentifierTable(), PP.getSelectorTable(),
215 PP.getBuiltinInfo());
216 Context.InitBuiltinTypes(*Target);
218 ASTConsumer Consumer;
219 Sema S(PP, Context, Consumer);
220 Parser P(PP, S, false);
221 PragmaOpenCLExtensionCallbacks* Callbacks = new PragmaOpenCLExtensionCallbacks;
222 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
225 PP.EnterMainSourceFile();
229 if (Tok.is(tok::eof))
233 PragmaOpenCLExtensionCallbacks::CallbackParameters RetVal = {
241 TEST_F(PPCallbacksTest, QuotedFilename) {
243 "#include \"quoted.h\"\n";
245 CharSourceRange Range =
246 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
248 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
251 TEST_F(PPCallbacksTest, AngledFilename) {
253 "#include <angled.h>\n";
255 CharSourceRange Range =
256 InclusionDirectiveFilenameRange(Source, "/angled.h", true);
258 ASSERT_EQ("<angled.h>", GetSourceString(Range));
261 TEST_F(PPCallbacksTest, QuotedInMacro) {
263 "#define MACRO_QUOTED \"quoted.h\"\n"
264 "#include MACRO_QUOTED\n";
266 CharSourceRange Range =
267 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
269 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
272 TEST_F(PPCallbacksTest, AngledInMacro) {
274 "#define MACRO_ANGLED <angled.h>\n"
275 "#include MACRO_ANGLED\n";
277 CharSourceRange Range =
278 InclusionDirectiveFilenameRange(Source, "/angled.h", true);
280 ASSERT_EQ("<angled.h>", GetSourceString(Range));
283 TEST_F(PPCallbacksTest, StringizedMacroArgument) {
285 "#define MACRO_STRINGIZED(x) #x\n"
286 "#include MACRO_STRINGIZED(quoted.h)\n";
288 CharSourceRange Range =
289 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
291 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
294 TEST_F(PPCallbacksTest, ConcatenatedMacroArgument) {
296 "#define MACRO_ANGLED <angled.h>\n"
297 "#define MACRO_CONCAT(x, y) x ## _ ## y\n"
298 "#include MACRO_CONCAT(MACRO, ANGLED)\n";
300 CharSourceRange Range =
301 InclusionDirectiveFilenameRange(Source, "/angled.h", false);
303 ASSERT_EQ("<angled.h>", GetSourceString(Range));
306 TEST_F(PPCallbacksTest, TrigraphFilename) {
308 "#include \"tri\?\?-graph.h\"\n";
310 CharSourceRange Range =
311 InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
313 ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
316 TEST_F(PPCallbacksTest, TrigraphInMacro) {
318 "#define MACRO_TRIGRAPH \"tri\?\?-graph.h\"\n"
319 "#include MACRO_TRIGRAPH\n";
321 CharSourceRange Range =
322 InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
324 ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
327 TEST_F(PPCallbacksTest, OpenCLExtensionPragmaEnabled) {
329 "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
331 PragmaOpenCLExtensionCallbacks::CallbackParameters Parameters =
332 PragmaOpenCLExtensionCall(Source);
334 ASSERT_EQ("cl_khr_fp64", Parameters.Name);
335 unsigned ExpectedState = 1;
336 ASSERT_EQ(ExpectedState, Parameters.State);
339 TEST_F(PPCallbacksTest, OpenCLExtensionPragmaDisabled) {
341 "#pragma OPENCL EXTENSION cl_khr_fp16 : disable\n";
343 PragmaOpenCLExtensionCallbacks::CallbackParameters Parameters =
344 PragmaOpenCLExtensionCall(Source);
346 ASSERT_EQ("cl_khr_fp16", Parameters.Name);
347 unsigned ExpectedState = 0;
348 ASSERT_EQ(ExpectedState, Parameters.State);
351 } // anonoymous namespace