2011-12-22 00:56:29 +08:00
|
|
|
//===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager tests ---===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-12-22 00:56:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-10-24 06:38:58 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Basic/TargetOptions.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2012-10-25 00:24:38 +08:00
|
|
|
#include "clang/Lex/HeaderSearchOptions.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-10-25 01:46:57 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-06-04 11:28:55 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2011-12-22 00:56:29 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// The test fixture.
|
|
|
|
class SourceManagerTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
SourceManagerTest()
|
|
|
|
: FileMgr(FileMgrOpts),
|
|
|
|
DiagID(new DiagnosticIDs()),
|
2012-10-24 06:31:51 +08:00
|
|
|
Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
|
2012-10-17 08:11:35 +08:00
|
|
|
SourceMgr(Diags, FileMgr),
|
|
|
|
TargetOpts(new TargetOptions) {
|
|
|
|
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
|
2014-07-06 13:26:44 +08:00
|
|
|
Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
|
2011-12-22 00:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemOptions FileMgrOpts;
|
|
|
|
FileManager FileMgr;
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
|
2011-12-22 00:56:29 +08:00
|
|
|
DiagnosticsEngine Diags;
|
|
|
|
SourceManager SourceMgr;
|
|
|
|
LangOptions LangOpts;
|
2014-07-06 13:26:44 +08:00
|
|
|
std::shared_ptr<TargetOptions> TargetOpts;
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<TargetInfo> Target;
|
2011-12-22 00:56:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
|
|
|
|
const char *source =
|
|
|
|
"#define M(x) [x]\n"
|
|
|
|
"M(foo)";
|
2016-03-05 03:00:41 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(source);
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
|
2014-05-21 09:12:41 +08:00
|
|
|
SourceMgr.setMainFileID(mainFileID);
|
2011-12-22 00:56:29 +08:00
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
TrivialModuleLoader ModLoader;
|
2017-01-06 09:04:46 +08:00
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, &*Target);
|
2017-01-06 03:11:36 +08:00
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
2019-03-10 01:33:56 +08:00
|
|
|
SourceMgr, HeaderInfo, ModLoader,
|
2014-06-08 16:38:12 +08:00
|
|
|
/*IILookup =*/nullptr,
|
2014-05-02 11:43:30 +08:00
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
PP.Initialize(*Target);
|
2011-12-22 00:56:29 +08:00
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
std::vector<Token> toks;
|
|
|
|
while (1) {
|
|
|
|
Token tok;
|
|
|
|
PP.Lex(tok);
|
|
|
|
if (tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
toks.push_back(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we got the tokens that we expected.
|
|
|
|
ASSERT_EQ(3U, toks.size());
|
|
|
|
ASSERT_EQ(tok::l_square, toks[0].getKind());
|
|
|
|
ASSERT_EQ(tok::identifier, toks[1].getKind());
|
|
|
|
ASSERT_EQ(tok::r_square, toks[2].getKind());
|
|
|
|
|
|
|
|
SourceLocation lsqrLoc = toks[0].getLocation();
|
|
|
|
SourceLocation idLoc = toks[1].getLocation();
|
|
|
|
SourceLocation rsqrLoc = toks[2].getLocation();
|
|
|
|
|
|
|
|
SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
|
|
|
|
SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
|
|
|
|
ASSERT_TRUE(macroExpStartLoc.isFileID());
|
|
|
|
ASSERT_TRUE(macroExpEndLoc.isFileID());
|
|
|
|
|
2012-02-13 20:32:26 +08:00
|
|
|
SmallString<32> str;
|
2011-12-22 00:56:29 +08:00
|
|
|
ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
|
|
|
|
ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
|
|
|
|
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
|
|
|
|
}
|
|
|
|
|
2012-06-19 11:09:38 +08:00
|
|
|
TEST_F(SourceManagerTest, getColumnNumber) {
|
|
|
|
const char *Source =
|
|
|
|
"int x;\n"
|
|
|
|
"int y;";
|
|
|
|
|
2016-03-05 03:00:41 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(Source);
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
|
2014-05-21 09:12:41 +08:00
|
|
|
SourceMgr.setMainFileID(MainFileID);
|
2012-06-19 11:09:38 +08:00
|
|
|
|
|
|
|
bool Invalid;
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
|
|
|
|
&Invalid));
|
|
|
|
EXPECT_TRUE(!Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
|
|
|
|
EXPECT_TRUE(Invalid);
|
|
|
|
|
|
|
|
// Test invalid files
|
|
|
|
Invalid = false;
|
|
|
|
SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
|
|
|
|
EXPECT_TRUE(Invalid);
|
|
|
|
|
|
|
|
Invalid = false;
|
|
|
|
SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
|
|
|
|
EXPECT_TRUE(Invalid);
|
|
|
|
|
|
|
|
// Test with no invalid flag.
|
2014-06-08 16:38:12 +08:00
|
|
|
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
|
2012-06-19 11:09:38 +08:00
|
|
|
}
|
|
|
|
|
2018-08-31 07:10:52 +08:00
|
|
|
TEST_F(SourceManagerTest, locationPrintTest) {
|
|
|
|
const char *header = "#define IDENTITY(x) x\n";
|
|
|
|
|
|
|
|
const char *Source = "int x;\n"
|
|
|
|
"include \"test-header.h\"\n"
|
|
|
|
"IDENTITY(int y);\n"
|
|
|
|
"int z;";
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(header);
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(Source);
|
|
|
|
|
|
|
|
const FileEntry *SourceFile =
|
|
|
|
FileMgr.getVirtualFile("/mainFile.cpp", Buf->getBufferSize(), 0);
|
|
|
|
SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
|
|
|
|
|
|
|
|
const FileEntry *HeaderFile =
|
|
|
|
FileMgr.getVirtualFile("/test-header.h", HeaderBuf->getBufferSize(), 0);
|
|
|
|
SourceMgr.overrideFileContents(HeaderFile, std::move(HeaderBuf));
|
|
|
|
|
|
|
|
FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User);
|
|
|
|
FileID HeaderFileID = SourceMgr.getOrCreateFileID(HeaderFile, SrcMgr::C_User);
|
|
|
|
SourceMgr.setMainFileID(MainFileID);
|
|
|
|
|
|
|
|
auto BeginLoc = SourceMgr.getLocForStartOfFile(MainFileID);
|
|
|
|
auto EndLoc = SourceMgr.getLocForEndOfFile(MainFileID);
|
|
|
|
|
|
|
|
auto BeginEOLLoc = SourceMgr.translateLineCol(MainFileID, 1, 7);
|
|
|
|
|
|
|
|
auto HeaderLoc = SourceMgr.getLocForStartOfFile(HeaderFileID);
|
|
|
|
|
|
|
|
EXPECT_EQ(BeginLoc.printToString(SourceMgr), "/mainFile.cpp:1:1");
|
|
|
|
EXPECT_EQ(EndLoc.printToString(SourceMgr), "/mainFile.cpp:4:7");
|
|
|
|
|
|
|
|
EXPECT_EQ(BeginEOLLoc.printToString(SourceMgr), "/mainFile.cpp:1:7");
|
|
|
|
EXPECT_EQ(HeaderLoc.printToString(SourceMgr), "/test-header.h:1:1");
|
|
|
|
|
|
|
|
EXPECT_EQ(SourceRange(BeginLoc, BeginLoc).printToString(SourceMgr),
|
|
|
|
"</mainFile.cpp:1:1>");
|
|
|
|
EXPECT_EQ(SourceRange(BeginLoc, BeginEOLLoc).printToString(SourceMgr),
|
|
|
|
"</mainFile.cpp:1:1, col:7>");
|
|
|
|
EXPECT_EQ(SourceRange(BeginLoc, EndLoc).printToString(SourceMgr),
|
|
|
|
"</mainFile.cpp:1:1, line:4:7>");
|
|
|
|
EXPECT_EQ(SourceRange(BeginLoc, HeaderLoc).printToString(SourceMgr),
|
|
|
|
"</mainFile.cpp:1:1, /test-header.h:1:1>");
|
|
|
|
}
|
|
|
|
|
2019-10-25 03:24:03 +08:00
|
|
|
TEST_F(SourceManagerTest, getInvalidBOM) {
|
|
|
|
ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM(""), nullptr);
|
|
|
|
ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("\x00\x00\x00"), nullptr);
|
|
|
|
ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("\xFF\xFF\xFF"), nullptr);
|
|
|
|
ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("#include <iostream>"),
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
"\xFE\xFF#include <iostream>")),
|
|
|
|
"UTF-16 (BE)");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
"\xFF\xFE#include <iostream>")),
|
|
|
|
"UTF-16 (LE)");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
"\x2B\x2F\x76#include <iostream>")),
|
|
|
|
"UTF-7");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
"\xF7\x64\x4C#include <iostream>")),
|
|
|
|
"UTF-1");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
"\xDD\x73\x66\x73#include <iostream>")),
|
|
|
|
"UTF-EBCDIC");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
"\x0E\xFE\xFF#include <iostream>")),
|
|
|
|
"SCSU");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
"\xFB\xEE\x28#include <iostream>")),
|
|
|
|
"BOCU-1");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
"\x84\x31\x95\x33#include <iostream>")),
|
|
|
|
"GB-18030");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
llvm::StringLiteral::withInnerNUL(
|
|
|
|
"\x00\x00\xFE\xFF#include <iostream>"))),
|
|
|
|
"UTF-32 (BE)");
|
|
|
|
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
|
|
|
|
llvm::StringLiteral::withInnerNUL(
|
|
|
|
"\xFF\xFE\x00\x00#include <iostream>"))),
|
|
|
|
"UTF-32 (LE)");
|
|
|
|
}
|
|
|
|
|
2011-12-22 00:56:35 +08:00
|
|
|
#if defined(LLVM_ON_UNIX)
|
|
|
|
|
|
|
|
TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
|
|
|
|
const char *header =
|
|
|
|
"#define FM(x,y) x\n";
|
|
|
|
|
|
|
|
const char *main =
|
|
|
|
"#include \"/test-header.h\"\n"
|
|
|
|
"#define VAL 0\n"
|
|
|
|
"FM(VAL,0)\n"
|
|
|
|
"FM(0,VAL)\n"
|
|
|
|
"FM(FM(0,VAL),0)\n"
|
|
|
|
"#define CONCAT(X, Y) X##Y\n"
|
|
|
|
"CONCAT(1,1)\n";
|
|
|
|
|
2016-03-05 03:00:41 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(header);
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> MainBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(main);
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
|
2014-05-21 09:12:41 +08:00
|
|
|
SourceMgr.setMainFileID(mainFileID);
|
2011-12-22 00:56:35 +08:00
|
|
|
|
|
|
|
const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
|
2014-08-28 04:03:29 +08:00
|
|
|
HeaderBuf->getBufferSize(), 0);
|
2014-08-28 04:54:45 +08:00
|
|
|
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
|
2011-12-22 00:56:35 +08:00
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
TrivialModuleLoader ModLoader;
|
2017-01-06 09:04:46 +08:00
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, &*Target);
|
2017-01-06 03:11:36 +08:00
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
2019-03-10 01:33:56 +08:00
|
|
|
SourceMgr, HeaderInfo, ModLoader,
|
2014-06-08 16:38:12 +08:00
|
|
|
/*IILookup =*/nullptr,
|
2014-05-02 11:43:30 +08:00
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
PP.Initialize(*Target);
|
2011-12-22 00:56:35 +08:00
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
std::vector<Token> toks;
|
|
|
|
while (1) {
|
|
|
|
Token tok;
|
|
|
|
PP.Lex(tok);
|
|
|
|
if (tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
toks.push_back(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we got the tokens that we expected.
|
|
|
|
ASSERT_EQ(4U, toks.size());
|
|
|
|
ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
|
|
|
|
ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
|
|
|
|
ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
|
|
|
|
ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
|
|
|
|
|
|
|
|
SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
|
|
|
|
SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
|
|
|
|
SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
|
|
|
|
SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
|
|
|
|
SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
|
|
|
|
defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
|
|
|
|
loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
|
|
|
|
loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
|
|
|
|
loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
|
|
|
|
defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
|
|
|
|
|
|
|
|
EXPECT_TRUE(defLoc.isFileID());
|
|
|
|
EXPECT_TRUE(loc1.isFileID());
|
|
|
|
EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
|
|
|
|
EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
|
|
|
|
EXPECT_EQ(loc2, toks[1].getLocation());
|
|
|
|
EXPECT_EQ(loc3, toks[2].getLocation());
|
|
|
|
EXPECT_TRUE(defLoc2.isFileID());
|
|
|
|
}
|
|
|
|
|
2012-03-28 02:47:48 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct MacroAction {
|
2017-04-27 05:05:44 +08:00
|
|
|
enum Kind { kExpansion, kDefinition, kUnDefinition};
|
|
|
|
|
2012-03-28 02:47:48 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
std::string Name;
|
2017-04-27 05:05:44 +08:00
|
|
|
unsigned MAKind : 3;
|
|
|
|
|
|
|
|
MacroAction(SourceLocation Loc, StringRef Name, unsigned K)
|
|
|
|
: Loc(Loc), Name(Name), MAKind(K) { }
|
|
|
|
|
|
|
|
bool isExpansion() const { return MAKind == kExpansion; }
|
|
|
|
bool isDefinition() const { return MAKind & kDefinition; }
|
|
|
|
bool isUnDefinition() const { return MAKind & kUnDefinition; }
|
2012-03-28 02:47:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class MacroTracker : public PPCallbacks {
|
|
|
|
std::vector<MacroAction> &Macros;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
|
2015-04-11 10:00:23 +08:00
|
|
|
|
|
|
|
void MacroDefined(const Token &MacroNameTok,
|
|
|
|
const MacroDirective *MD) override {
|
2013-02-24 08:05:14 +08:00
|
|
|
Macros.push_back(MacroAction(MD->getLocation(),
|
2012-03-28 02:47:48 +08:00
|
|
|
MacroNameTok.getIdentifierInfo()->getName(),
|
2017-04-27 05:05:44 +08:00
|
|
|
MacroAction::kDefinition));
|
|
|
|
}
|
|
|
|
void MacroUndefined(const Token &MacroNameTok,
|
|
|
|
const MacroDefinition &MD,
|
|
|
|
const MacroDirective *UD) override {
|
|
|
|
Macros.push_back(
|
|
|
|
MacroAction(UD ? UD->getLocation() : SourceLocation(),
|
|
|
|
MacroNameTok.getIdentifierInfo()->getName(),
|
|
|
|
UD ? MacroAction::kDefinition | MacroAction::kUnDefinition
|
|
|
|
: MacroAction::kUnDefinition));
|
2012-03-28 02:47:48 +08:00
|
|
|
}
|
2015-05-04 11:15:40 +08:00
|
|
|
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
|
2015-04-11 10:00:23 +08:00
|
|
|
SourceRange Range, const MacroArgs *Args) override {
|
2012-03-28 02:47:48 +08:00
|
|
|
Macros.push_back(MacroAction(MacroNameTok.getLocation(),
|
|
|
|
MacroNameTok.getIdentifierInfo()->getName(),
|
2017-04-27 05:05:44 +08:00
|
|
|
MacroAction::kExpansion));
|
2012-03-28 02:47:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
|
|
|
|
const char *header =
|
2017-04-27 05:05:44 +08:00
|
|
|
"#define MACRO_IN_INCLUDE 0\n"
|
|
|
|
"#define MACRO_DEFINED\n"
|
|
|
|
"#undef MACRO_DEFINED\n"
|
|
|
|
"#undef MACRO_UNDEFINED\n";
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
const char *main =
|
|
|
|
"#define M(x) x\n"
|
|
|
|
"#define INC \"/test-header.h\"\n"
|
|
|
|
"#include M(INC)\n"
|
|
|
|
"#define INC2 </test-header.h>\n"
|
|
|
|
"#include M(INC2)\n";
|
|
|
|
|
2016-03-05 03:00:41 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(header);
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> MainBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(main);
|
2014-08-29 15:59:55 +08:00
|
|
|
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
|
2014-08-28 04:03:29 +08:00
|
|
|
HeaderBuf->getBufferSize(), 0);
|
2014-08-28 04:54:45 +08:00
|
|
|
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
|
2012-03-28 02:47:48 +08:00
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
TrivialModuleLoader ModLoader;
|
2017-01-06 09:04:46 +08:00
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, &*Target);
|
2017-01-06 03:11:36 +08:00
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
2019-03-10 01:33:56 +08:00
|
|
|
SourceMgr, HeaderInfo, ModLoader,
|
2014-06-08 16:38:12 +08:00
|
|
|
/*IILookup =*/nullptr,
|
2014-05-02 11:43:30 +08:00
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
PP.Initialize(*Target);
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
std::vector<MacroAction> Macros;
|
2019-08-15 07:04:18 +08:00
|
|
|
PP.addPPCallbacks(std::make_unique<MacroTracker>(Macros));
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
std::vector<Token> toks;
|
|
|
|
while (1) {
|
|
|
|
Token tok;
|
|
|
|
PP.Lex(tok);
|
|
|
|
if (tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
toks.push_back(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we got the tokens that we expected.
|
|
|
|
ASSERT_EQ(0U, toks.size());
|
|
|
|
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_EQ(15U, Macros.size());
|
2012-03-28 02:47:48 +08:00
|
|
|
// #define M(x) x
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[0].isDefinition());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("M", Macros[0].Name);
|
|
|
|
// #define INC "/test-header.h"
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[1].isDefinition());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("INC", Macros[1].Name);
|
|
|
|
// M expansion in #include M(INC)
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_FALSE(Macros[2].isDefinition());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("M", Macros[2].Name);
|
|
|
|
// INC expansion in #include M(INC)
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[3].isExpansion());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("INC", Macros[3].Name);
|
|
|
|
// #define MACRO_IN_INCLUDE 0
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[4].isDefinition());
|
2012-03-28 02:47:48 +08:00
|
|
|
ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
|
2017-04-27 05:05:44 +08:00
|
|
|
// #define MACRO_DEFINED
|
|
|
|
ASSERT_TRUE(Macros[5].isDefinition());
|
|
|
|
ASSERT_FALSE(Macros[5].isUnDefinition());
|
|
|
|
ASSERT_EQ("MACRO_DEFINED", Macros[5].Name);
|
|
|
|
// #undef MACRO_DEFINED
|
|
|
|
ASSERT_TRUE(Macros[6].isDefinition());
|
|
|
|
ASSERT_TRUE(Macros[6].isUnDefinition());
|
|
|
|
ASSERT_EQ("MACRO_DEFINED", Macros[6].Name);
|
|
|
|
// #undef MACRO_UNDEFINED
|
|
|
|
ASSERT_FALSE(Macros[7].isDefinition());
|
|
|
|
ASSERT_TRUE(Macros[7].isUnDefinition());
|
|
|
|
ASSERT_EQ("MACRO_UNDEFINED", Macros[7].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
// #define INC2 </test-header.h>
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[8].isDefinition());
|
|
|
|
ASSERT_EQ("INC2", Macros[8].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
// M expansion in #include M(INC2)
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_FALSE(Macros[9].isDefinition());
|
|
|
|
ASSERT_EQ("M", Macros[9].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
// INC2 expansion in #include M(INC2)
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[10].isExpansion());
|
|
|
|
ASSERT_EQ("INC2", Macros[10].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
// #define MACRO_IN_INCLUDE 0
|
2017-04-27 05:05:44 +08:00
|
|
|
ASSERT_TRUE(Macros[11].isDefinition());
|
|
|
|
ASSERT_EQ("MACRO_IN_INCLUDE", Macros[11].Name);
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
// The INC expansion in #include M(INC) comes before the first
|
|
|
|
// MACRO_IN_INCLUDE definition of the included file.
|
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
|
|
|
|
|
|
|
|
// The INC2 expansion in #include M(INC2) comes before the second
|
|
|
|
// MACRO_IN_INCLUDE definition of the included file.
|
2017-04-27 05:05:44 +08:00
|
|
|
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[10].Loc, Macros[11].Loc));
|
2012-03-28 02:47:48 +08:00
|
|
|
}
|
|
|
|
|
2011-12-22 00:56:35 +08:00
|
|
|
#endif
|
|
|
|
|
2011-12-22 00:56:29 +08:00
|
|
|
} // anonymous namespace
|