//===- unittests/AST/CommentLexer.cpp ------ Comment lexer tests ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/CommentLexer.h"
#include "llvm/ADT/STLExtras.h"
#include <vector>
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace clang {
namespace comments {
namespace {
class CommentLexerTest : public ::testing::Test {
protected:
CommentLexerTest()
: FileMgr(FileMgrOpts),
DiagID(new DiagnosticIDs()),
Diags(DiagID, new IgnoringDiagConsumer()),
SourceMgr(Diags, FileMgr) {
}
FileSystemOptions FileMgrOpts;
FileManager FileMgr;
IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
DiagnosticsEngine Diags;
SourceManager SourceMgr;
void lexString(const char *Source, std::vector<Token> &Toks);
};
void CommentLexerTest::lexString(const char *Source,
std::vector<Token> &Toks) {
MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source);
FileID File = SourceMgr.createFileIDForMemBuffer(Buf);
SourceLocation Begin = SourceMgr.getLocForStartOfFile(File);
comments::Lexer L(Begin, CommentOptions(),
Source, Source + strlen(Source));
while (1) {
Token Tok;
L.lex(Tok);
if (Tok.is(tok::eof))
break;
Toks.push_back(Tok);
}
}
} // unnamed namespace
// Empty source range should be handled.
TEST_F(CommentLexerTest, Basic1) {
const char *Source = "";
std::vector<Token> Toks;
lexString(Source, Toks);
ASSERT_EQ(0U, Toks.size());
}
// Empty comments should be handled.
TEST_F(CommentLexerTest, Basic2) {
const char *Sources[] = {
"//", "///", "//!", "///<", "//!<"
};
for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
std::vector<Token> Toks;
lexString(Sources[i], Toks);
ASSERT_EQ(1U, Toks.size());
ASSERT_EQ(tok::newline, Toks[0].getKind());
}
}
// Empty comments should be handled.
TEST_F(CommentLexerTest, Basic3) {
const char *Sources[] = {
"/**/", "/***/", "/*!*/", "/**<*/", "/*!<*/"
};
for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
std::vector<Token> Toks;
lexString(Sources[i], Toks);
ASSERT_EQ(2U, Toks.size());
ASSERT_EQ(tok::newline, Toks[0].getKind());
ASSERT_EQ(tok::newline, Toks[1].getKind());
}
}
// Single comment with plain text.
TEST_F(CommentLexerTest, Basic4) {
const char *Sources[] = {
"// Meow", "/// Meow", "//! Meow",
"// Meow\n", "// Meow\r\n", "//! Meow\r",
};
for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
std::vector<Token> Toks;
lexString(Sources[i], Toks);
ASSERT_EQ(2U, Toks.size());
ASSERT_EQ(tok::text, Toks[0].getKind());
ASSERT_EQ(StringRef(" Meow"), Toks[0].getText());
ASSERT_EQ(tok::newline, Toks[1].getKind());
}
}
// Single comment with plain text.
TEST_F(CommentLexerTest, Basic5) {
const char *Sources[] = {
"/* Meow*/", "/** Meow*/", "/*! Meow*/"
};
for (size_t i = 0, e = array_lengthof(Sources); i != e;