2012-12-04 15:27:05 +08:00
|
|
|
//===- unittests/Lex/PPConditionalDirectiveRecordTest.cpp-PP directive tests =//
|
2012-03-05 13:48:17 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Lex/PPConditionalDirectiveRecord.h"
|
2012-03-05 13:48:17 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-10-24 06:43:37 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2012-03-05 13:48:17 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-12-04 17:53:37 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-03-05 13:48:17 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Basic/TargetOptions.h"
|
2012-03-05 13:48:17 +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"
|
2012-03-05 13:48:17 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-10-25 01:46:57 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2012-03-05 13:48:17 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// The test fixture.
|
2012-12-04 15:27:05 +08:00
|
|
|
class PPConditionalDirectiveRecordTest : public ::testing::Test {
|
2012-03-05 13:48:17 +08:00
|
|
|
protected:
|
2012-12-04 15:27:05 +08:00
|
|
|
PPConditionalDirectiveRecordTest()
|
2012-03-05 13:48:17 +08:00
|
|
|
: FileMgr(FileMgrOpts),
|
|
|
|
DiagID(new DiagnosticIDs()),
|
2012-10-24 06:43:37 +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";
|
2012-11-16 12:40:11 +08:00
|
|
|
Target = TargetInfo::CreateTargetInfo(Diags, &*TargetOpts);
|
2012-03-05 13:48:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemOptions FileMgrOpts;
|
|
|
|
FileManager FileMgr;
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
|
|
|
|
DiagnosticsEngine Diags;
|
|
|
|
SourceManager SourceMgr;
|
|
|
|
LangOptions LangOpts;
|
2012-10-17 08:11:35 +08:00
|
|
|
IntrusiveRefCntPtr<TargetOptions> TargetOpts;
|
2012-03-05 13:48:17 +08:00
|
|
|
IntrusiveRefCntPtr<TargetInfo> Target;
|
|
|
|
};
|
|
|
|
|
|
|
|
class VoidModuleLoader : public ModuleLoader {
|
2014-04-24 03:04:32 +08:00
|
|
|
ModuleLoadResult loadModule(SourceLocation ImportLoc,
|
|
|
|
ModuleIdPath Path,
|
|
|
|
Module::NameVisibilityKind Visibility,
|
|
|
|
bool IsInclusionDirective) override {
|
2012-11-30 08:01:57 +08:00
|
|
|
return ModuleLoadResult();
|
2012-03-05 13:48:17 +08:00
|
|
|
}
|
2013-01-12 10:16:29 +08:00
|
|
|
|
2014-04-24 03:04:32 +08:00
|
|
|
void makeModuleVisible(Module *Mod,
|
|
|
|
Module::NameVisibilityKind Visibility,
|
|
|
|
SourceLocation ImportLoc,
|
|
|
|
bool Complain) override { }
|
2014-04-23 20:57:01 +08:00
|
|
|
|
2014-04-24 03:04:32 +08:00
|
|
|
GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
|
2014-06-08 16:38:12 +08:00
|
|
|
{ return nullptr; }
|
2014-04-24 03:04:32 +08:00
|
|
|
bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
|
|
|
|
{ return 0; };
|
2012-03-05 13:48:17 +08:00
|
|
|
};
|
|
|
|
|
2012-12-04 15:27:05 +08:00
|
|
|
TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
|
2012-03-05 13:48:17 +08:00
|
|
|
const char *source =
|
|
|
|
"0 1\n"
|
|
|
|
"#if 1\n"
|
|
|
|
"2\n"
|
|
|
|
"#ifndef BB\n"
|
|
|
|
"3 4\n"
|
|
|
|
"#else\n"
|
|
|
|
"#endif\n"
|
|
|
|
"5\n"
|
|
|
|
"#endif\n"
|
|
|
|
"6\n"
|
|
|
|
"#if 1\n"
|
|
|
|
"7\n"
|
|
|
|
"#if 1\n"
|
|
|
|
"#endif\n"
|
|
|
|
"8\n"
|
|
|
|
"#endif\n"
|
|
|
|
"9\n";
|
|
|
|
|
|
|
|
MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
|
2014-05-21 09:12:41 +08:00
|
|
|
SourceMgr.setMainFileID(SourceMgr.createFileID(buf));
|
2012-03-05 13:48:17 +08:00
|
|
|
|
|
|
|
VoidModuleLoader ModLoader;
|
Use the same SourceManager for ModuleMaps and compilations.
This allows using virtual file mappings on the original SourceManager to
map in virtual module.map files. Without this patch, the ModuleMap
search will find a module.map file (as the FileEntry exists in the
FileManager), but will be unable to get the content from the
SourceManager (as ModuleMap previously created its own SourceManager).
Two problems needed to be fixed which this patch exposed:
1. Storing the inferred module map
When writing out a module, the ASTWriter stores the names of the files
in the main source manager; when loading the AST again, the ASTReader
errs out if such a file is found missing, unless it is overridden.
Previously CompilerInstance's compileModule method would store the
inferred module map to a temporary file; the problem with this approach
is that now that the module map is handled by the main source manager,
the ASTWriter stores the name of the temporary module map as source to
the compilation; later, when the module is loaded, the temporary file
has already been deleted, which leads to a compilation error. This patch
changes the inferred module map to instead inject a virtual file into
the source manager. This both saves some disk IO, and works with how the
ASTWriter/ASTReader handle overridden source files.
2. Changing test input in test/Modules/Inputs/*
Now that the module map file is handled by the main source manager, the
VerifyDiagnosticConsumer will not ignore diagnostics created while
parsing the module map file. The module test test/Modules/renamed.m uses
-I test/Modules/Inputs and triggers recursive loading of all module maps
in test/Modules/Inputs, some of which had conflicting names, thus
leading errors while parsing the module maps. Those diagnostics already
occur on trunk, but before this patch they would not break the test, as
they were ignored by the VerifyDiagnosticConsumer. This patch thus
changes the module maps that have been recently introduced which broke
the invariant of compatible modules maps in test/Modules/Inputs.
llvm-svn: 193314
2013-10-24 15:51:24 +08:00
|
|
|
HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
|
2012-10-25 00:24:38 +08:00
|
|
|
Target.getPtr());
|
2014-05-02 11:43:38 +08:00
|
|
|
Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, 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-12-04 15:27:05 +08:00
|
|
|
PPConditionalDirectiveRecord *
|
|
|
|
PPRec = new PPConditionalDirectiveRecord(SourceMgr);
|
|
|
|
PP.addPPCallbacks(PPRec);
|
2012-03-05 13:48:17 +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(10U, toks.size());
|
|
|
|
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[0].getLocation(), toks[1].getLocation())));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[0].getLocation(), toks[2].getLocation())));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[3].getLocation(), toks[4].getLocation())));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[1].getLocation(), toks[5].getLocation())));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[2].getLocation(), toks[6].getLocation())));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[2].getLocation(), toks[5].getLocation())));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[0].getLocation(), toks[6].getLocation())));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[2].getLocation(), toks[8].getLocation())));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
|
2012-03-05 13:48:17 +08:00
|
|
|
SourceRange(toks[0].getLocation(), toks[9].getLocation())));
|
|
|
|
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
|
2012-03-05 13:48:17 +08:00
|
|
|
toks[0].getLocation(), toks[2].getLocation()));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
|
2012-03-05 13:48:17 +08:00
|
|
|
toks[3].getLocation(), toks[4].getLocation()));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
|
2012-03-05 13:48:17 +08:00
|
|
|
toks[1].getLocation(), toks[5].getLocation()));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
|
2012-03-05 13:48:17 +08:00
|
|
|
toks[2].getLocation(), toks[0].getLocation()));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
|
2012-03-05 13:48:17 +08:00
|
|
|
toks[4].getLocation(), toks[3].getLocation()));
|
2012-12-04 15:27:05 +08:00
|
|
|
EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
|
2012-03-05 13:48:17 +08:00
|
|
|
toks[5].getLocation(), toks[1].getLocation()));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|