blob: 7d9e9e9cbc7570a52f95aa2417fed25a8f81f1d0 [file] [log] [blame]
Argyrios Kyrtzidis19d5aea2012-01-19 15:59:01 +00001//===- unittests/Basic/LexerTest.cpp ------ Lexer 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
10#include "clang/Basic/SourceManager.h"
11#include "clang/Basic/FileManager.h"
12#include "clang/Basic/Diagnostic.h"
13#include "clang/Basic/LangOptions.h"
14#include "clang/Basic/TargetOptions.h"
15#include "clang/Basic/TargetInfo.h"
16#include "clang/Lex/ModuleLoader.h"
17#include "clang/Lex/HeaderSearch.h"
18#include "clang/Lex/Preprocessor.h"
19#include "llvm/Config/config.h"
20
21#include "gtest/gtest.h"
22
23using namespace llvm;
24using namespace clang;
25
26namespace {
27
28// The test fixture.
29class LexerTest : public ::testing::Test {
30protected:
31 LexerTest()
32 : FileMgr(FileMgrOpts),
33 DiagID(new DiagnosticIDs()),
34 Diags(DiagID, new IgnoringDiagConsumer()),
35 SourceMgr(Diags, FileMgr) {
36 TargetOpts.Triple = "x86_64-apple-darwin11.1.0";
37 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
38 }
39
40 FileSystemOptions FileMgrOpts;
41 FileManager FileMgr;
42 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
43 DiagnosticsEngine Diags;
44 SourceManager SourceMgr;
45 LangOptions LangOpts;
46 TargetOptions TargetOpts;
47 llvm::IntrusiveRefCntPtr<TargetInfo> Target;
48};
49
50class VoidModuleLoader : public ModuleLoader {
51 virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
52 Module::NameVisibilityKind Visibility,
53 bool IsInclusionDirective) {
54 return 0;
55 }
56};
57
58TEST_F(LexerTest, LexAPI) {
59 const char *source =
60 "#define M(x) [x]\n"
Argyrios Kyrtzidisd9806c92012-01-20 16:52:43 +000061 "#define N(x) x\n"
62 "M(foo) N([bar])";
Argyrios Kyrtzidis19d5aea2012-01-19 15:59:01 +000063 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
Benjamin Kramer65eccf02012-01-19 21:12:23 +000064 SourceMgr.createMainFileIDForMemBuffer(buf);
Argyrios Kyrtzidis19d5aea2012-01-19 15:59:01 +000065
66 VoidModuleLoader ModLoader;
67 HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts);
68 Preprocessor PP(Diags, LangOpts,
69 Target.getPtr(),
70 SourceMgr, HeaderInfo, ModLoader,
71 /*IILookup =*/ 0,
72 /*OwnsHeaderSearch =*/false,
73 /*DelayInitialization =*/ false);
74 PP.EnterMainSourceFile();
75
76 std::vector<Token> toks;
77 while (1) {
78 Token tok;
79 PP.Lex(tok);
80 if (tok.is(tok::eof))
81 break;
82 toks.push_back(tok);
83 }
84
85 // Make sure we got the tokens that we expected.
Argyrios Kyrtzidisd9806c92012-01-20 16:52:43 +000086 ASSERT_EQ(6U, toks.size());
Argyrios Kyrtzidis19d5aea2012-01-19 15:59:01 +000087 ASSERT_EQ(tok::l_square, toks[0].getKind());
88 ASSERT_EQ(tok::identifier, toks[1].getKind());
89 ASSERT_EQ(tok::r_square, toks[2].getKind());
Argyrios Kyrtzidisd9806c92012-01-20 16:52:43 +000090 ASSERT_EQ(tok::l_square, toks[3].getKind());
91 ASSERT_EQ(tok::identifier, toks[4].getKind());
92 ASSERT_EQ(tok::r_square, toks[5].getKind());
Argyrios Kyrtzidis19d5aea2012-01-19 15:59:01 +000093
94 SourceLocation lsqrLoc = toks[0].getLocation();
95 SourceLocation idLoc = toks[1].getLocation();
96 SourceLocation rsqrLoc = toks[2].getLocation();
Argyrios Kyrtzidis11b652d2012-01-19 15:59:14 +000097 std::pair<SourceLocation,SourceLocation>
98 macroPair = SourceMgr.getExpansionRange(lsqrLoc);
99 SourceRange macroRange = SourceRange(macroPair.first, macroPair.second);
100
Argyrios Kyrtzidis69bda4c2012-01-19 15:59:08 +0000101 SourceLocation Loc;
102 EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc));
Argyrios Kyrtzidis11b652d2012-01-19 15:59:14 +0000103 EXPECT_EQ(Loc, macroRange.getBegin());
Argyrios Kyrtzidis19d5aea2012-01-19 15:59:01 +0000104 EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts));
105 EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts));
Argyrios Kyrtzidis69bda4c2012-01-19 15:59:08 +0000106 EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc));
Argyrios Kyrtzidis11b652d2012-01-19 15:59:14 +0000107 EXPECT_EQ(Loc, macroRange.getEnd());
108
109 CharSourceRange range = Lexer::makeFileCharRange(SourceRange(lsqrLoc, idLoc),
110 SourceMgr, LangOpts);
111 EXPECT_TRUE(range.isInvalid());
112 range = Lexer::makeFileCharRange(SourceRange(idLoc, rsqrLoc),
113 SourceMgr, LangOpts);
114 EXPECT_TRUE(range.isInvalid());
115 range = Lexer::makeFileCharRange(SourceRange(lsqrLoc, rsqrLoc),
116 SourceMgr, LangOpts);
117 EXPECT_TRUE(!range.isTokenRange());
118 EXPECT_EQ(range.getAsRange(),
119 SourceRange(macroRange.getBegin(),
120 macroRange.getEnd().getLocWithOffset(1)));
Argyrios Kyrtzidise64d9032012-01-19 15:59:19 +0000121
122 StringRef text = Lexer::getSourceText(
123 CharSourceRange::getTokenRange(SourceRange(lsqrLoc, rsqrLoc)),
124 SourceMgr, LangOpts);
125 EXPECT_EQ(text, "M(foo)");
Argyrios Kyrtzidisd9806c92012-01-20 16:52:43 +0000126
127 SourceLocation macroLsqrLoc = toks[3].getLocation();
128 SourceLocation macroIdLoc = toks[4].getLocation();
129 SourceLocation macroRsqrLoc = toks[5].getLocation();
130 SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc);
131 SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc);
132 SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc);
133
134 range = Lexer::makeFileCharRange(SourceRange(macroLsqrLoc, macroIdLoc),
135 SourceMgr, LangOpts);
136 EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)),
137 range.getAsRange());
138
139 range = Lexer::makeFileCharRange(SourceRange(macroIdLoc, macroRsqrLoc),
140 SourceMgr, LangOpts);
141 EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)),
142 range.getAsRange());
143
144 macroPair = SourceMgr.getExpansionRange(macroLsqrLoc);
145 range = Lexer::makeFileCharRange(SourceRange(macroLsqrLoc, macroRsqrLoc),
146 SourceMgr, LangOpts);
147 EXPECT_EQ(SourceRange(macroPair.first, macroPair.second.getLocWithOffset(1)),
148 range.getAsRange());
149
150 text = Lexer::getSourceText(
151 CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)),
152 SourceMgr, LangOpts);
153 EXPECT_EQ(text, "[bar");
Argyrios Kyrtzidis19d5aea2012-01-19 15:59:01 +0000154}
155
156} // anonymous namespace