2012-11-02 01:52:58 +08:00
|
|
|
//===- unittests/Lex/PPCallbacksTest.cpp - PPCallbacks 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
|
2012-11-02 01:52:58 +08:00
|
|
|
//
|
|
|
|
//===--------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-11-02 01:52:58 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2014-05-11 00:31:55 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-11-02 01:52:58 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/Basic/TargetOptions.h"
|
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/HeaderSearchOptions.h"
|
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2013-10-12 17:29:48 +08:00
|
|
|
#include "clang/Parse/Parser.h"
|
|
|
|
#include "clang/Sema/Sema.h"
|
2012-11-02 01:52:58 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2013-06-12 06:15:02 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2012-11-02 01:52:58 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Stub to collect data from InclusionDirective callbacks.
|
|
|
|
class InclusionDirectiveCallbacks : public PPCallbacks {
|
|
|
|
public:
|
2015-04-11 10:00:23 +08:00
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
|
|
|
StringRef SearchPath, StringRef RelativePath,
|
2018-05-11 03:05:36 +08:00
|
|
|
const Module *Imported,
|
|
|
|
SrcMgr::CharacteristicKind FileType) override {
|
|
|
|
this->HashLoc = HashLoc;
|
|
|
|
this->IncludeTok = IncludeTok;
|
|
|
|
this->FileName = FileName.str();
|
|
|
|
this->IsAngled = IsAngled;
|
|
|
|
this->FilenameRange = FilenameRange;
|
|
|
|
this->File = File;
|
|
|
|
this->SearchPath = SearchPath.str();
|
|
|
|
this->RelativePath = RelativePath.str();
|
|
|
|
this->Imported = Imported;
|
|
|
|
this->FileType = FileType;
|
2012-11-02 01:52:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation HashLoc;
|
|
|
|
Token IncludeTok;
|
|
|
|
SmallString<16> FileName;
|
|
|
|
bool IsAngled;
|
|
|
|
CharSourceRange FilenameRange;
|
|
|
|
const FileEntry* File;
|
|
|
|
SmallString<16> SearchPath;
|
|
|
|
SmallString<16> RelativePath;
|
|
|
|
const Module* Imported;
|
2018-05-11 03:05:36 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType;
|
2012-11-02 01:52:58 +08:00
|
|
|
};
|
|
|
|
|
2019-01-11 05:22:13 +08:00
|
|
|
class CondDirectiveCallbacks : public PPCallbacks {
|
|
|
|
public:
|
|
|
|
struct Result {
|
|
|
|
SourceRange ConditionRange;
|
|
|
|
ConditionValueKind ConditionValue;
|
|
|
|
|
|
|
|
Result(SourceRange R, ConditionValueKind K)
|
|
|
|
: ConditionRange(R), ConditionValue(K) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Result> Results;
|
|
|
|
|
|
|
|
void If(SourceLocation Loc, SourceRange ConditionRange,
|
|
|
|
ConditionValueKind ConditionValue) override {
|
|
|
|
Results.emplace_back(ConditionRange, ConditionValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Elif(SourceLocation Loc, SourceRange ConditionRange,
|
|
|
|
ConditionValueKind ConditionValue, SourceLocation IfLoc) override {
|
|
|
|
Results.emplace_back(ConditionRange, ConditionValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-12 17:29:48 +08:00
|
|
|
// Stub to collect data from PragmaOpenCLExtension callbacks.
|
|
|
|
class PragmaOpenCLExtensionCallbacks : public PPCallbacks {
|
|
|
|
public:
|
|
|
|
typedef struct {
|
2013-10-14 15:13:59 +08:00
|
|
|
SmallString<16> Name;
|
2013-10-12 17:29:48 +08:00
|
|
|
unsigned State;
|
|
|
|
} CallbackParameters;
|
|
|
|
|
2015-07-23 04:46:26 +08:00
|
|
|
PragmaOpenCLExtensionCallbacks() : Name("Not called."), State(99) {}
|
2013-10-12 17:29:48 +08:00
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
void PragmaOpenCLExtension(clang::SourceLocation NameLoc,
|
|
|
|
const clang::IdentifierInfo *Name,
|
|
|
|
clang::SourceLocation StateLoc,
|
|
|
|
unsigned State) override {
|
2013-10-12 17:29:48 +08:00
|
|
|
this->NameLoc = NameLoc;
|
2013-10-14 15:13:59 +08:00
|
|
|
this->Name = Name->getName();
|
2013-10-12 17:29:48 +08:00
|
|
|
this->StateLoc = StateLoc;
|
|
|
|
this->State = State;
|
2015-07-23 04:46:26 +08:00
|
|
|
}
|
2013-10-12 17:29:48 +08:00
|
|
|
|
|
|
|
SourceLocation NameLoc;
|
2013-10-14 15:13:59 +08:00
|
|
|
SmallString<16> Name;
|
2013-10-12 17:29:48 +08:00
|
|
|
SourceLocation StateLoc;
|
|
|
|
unsigned State;
|
|
|
|
};
|
|
|
|
|
2021-07-03 03:43:01 +08:00
|
|
|
class PragmaMarkCallbacks : public PPCallbacks {
|
|
|
|
public:
|
|
|
|
struct Mark {
|
|
|
|
SourceLocation Location;
|
|
|
|
std::string Trivia;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Mark> Marks;
|
|
|
|
|
|
|
|
void PragmaMark(SourceLocation Loc, StringRef Trivia) override {
|
|
|
|
Marks.emplace_back(Mark{Loc, Trivia.str()});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-02 01:52:58 +08:00
|
|
|
// PPCallbacks test fixture.
|
|
|
|
class PPCallbacksTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
PPCallbacksTest()
|
2018-10-10 21:27:25 +08:00
|
|
|
: InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
|
2015-10-08 22:20:14 +08:00
|
|
|
FileMgr(FileSystemOptions(), InMemoryFileSystem),
|
|
|
|
DiagID(new DiagnosticIDs()), DiagOpts(new DiagnosticOptions()),
|
2014-07-06 13:26:44 +08:00
|
|
|
Diags(DiagID, DiagOpts.get(), new IgnoringDiagConsumer()),
|
|
|
|
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions()) {
|
2012-11-02 01:52:58 +08:00
|
|
|
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
|
2014-07-06 13:26:44 +08:00
|
|
|
Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
|
2012-11-02 01:52:58 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 21:27:25 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem;
|
2012-11-02 01:52:58 +08:00
|
|
|
FileManager FileMgr;
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
|
|
|
|
DiagnosticsEngine Diags;
|
|
|
|
SourceManager SourceMgr;
|
|
|
|
LangOptions LangOpts;
|
2014-07-06 13:26:44 +08:00
|
|
|
std::shared_ptr<TargetOptions> TargetOpts;
|
2012-11-02 01:52:58 +08:00
|
|
|
IntrusiveRefCntPtr<TargetInfo> Target;
|
|
|
|
|
|
|
|
// Register a header path as a known file and add its location
|
|
|
|
// to search path.
|
2018-10-10 21:27:25 +08:00
|
|
|
void AddFakeHeader(HeaderSearch &HeaderInfo, const char *HeaderPath,
|
|
|
|
bool IsSystemHeader) {
|
|
|
|
// Tell FileMgr about header.
|
|
|
|
InMemoryFileSystem->addFile(HeaderPath, 0,
|
|
|
|
llvm::MemoryBuffer::getMemBuffer("\n"));
|
|
|
|
|
|
|
|
// Add header's parent path to search path.
|
|
|
|
StringRef SearchPath = llvm::sys::path::parent_path(HeaderPath);
|
2019-08-31 09:26:04 +08:00
|
|
|
auto DE = FileMgr.getOptionalDirectoryRef(SearchPath);
|
2019-08-02 05:31:56 +08:00
|
|
|
DirectoryLookup DL(*DE, SrcMgr::C_User, false);
|
2018-10-10 21:27:25 +08:00
|
|
|
HeaderInfo.AddSearchPath(DL, IsSystemHeader);
|
2012-11-02 01:52:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the raw source string of the range.
|
|
|
|
StringRef GetSourceString(CharSourceRange Range) {
|
|
|
|
const char* B = SourceMgr.getCharacterData(Range.getBegin());
|
|
|
|
const char* E = SourceMgr.getCharacterData(Range.getEnd());
|
|
|
|
|
|
|
|
return StringRef(B, E - B);
|
|
|
|
}
|
|
|
|
|
2019-01-11 05:22:13 +08:00
|
|
|
StringRef GetSourceStringToEnd(CharSourceRange Range) {
|
|
|
|
const char *B = SourceMgr.getCharacterData(Range.getBegin());
|
|
|
|
const char *E = SourceMgr.getCharacterData(Range.getEnd());
|
|
|
|
|
|
|
|
return StringRef(
|
|
|
|
B,
|
|
|
|
E - B + Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr, LangOpts));
|
|
|
|
}
|
|
|
|
|
2012-11-02 01:52:58 +08:00
|
|
|
// Run lexer over SourceText and collect FilenameRange from
|
|
|
|
// the InclusionDirective callback.
|
2018-10-10 21:27:25 +08:00
|
|
|
CharSourceRange InclusionDirectiveFilenameRange(const char *SourceText,
|
|
|
|
const char *HeaderPath,
|
|
|
|
bool SystemHeader) {
|
2014-10-16 06:00:40 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(SourceText);
|
2014-08-29 15:59:55 +08:00
|
|
|
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
|
2012-11-02 01:52:58 +08:00
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
TrivialModuleLoader ModLoader;
|
2012-11-02 01:52:58 +08:00
|
|
|
|
2017-01-06 09:04:46 +08:00
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, Target.get());
|
2012-11-02 01:52:58 +08:00
|
|
|
AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader);
|
|
|
|
|
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:38 +08:00
|
|
|
/*OwnsHeaderSearch =*/false);
|
2018-05-11 03:05:36 +08:00
|
|
|
return InclusionDirectiveCallback(PP)->FilenameRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrcMgr::CharacteristicKind InclusionDirectiveCharacteristicKind(
|
|
|
|
const char *SourceText, const char *HeaderPath, bool SystemHeader) {
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(SourceText);
|
|
|
|
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
|
|
|
|
|
|
|
|
TrivialModuleLoader ModLoader;
|
|
|
|
|
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, Target.get());
|
|
|
|
AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader);
|
|
|
|
|
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
2019-03-10 01:33:56 +08:00
|
|
|
SourceMgr, HeaderInfo, ModLoader,
|
2018-05-11 03:05:36 +08:00
|
|
|
/*IILookup =*/nullptr,
|
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
return InclusionDirectiveCallback(PP)->FileType;
|
|
|
|
}
|
|
|
|
|
|
|
|
InclusionDirectiveCallbacks *InclusionDirectiveCallback(Preprocessor &PP) {
|
2014-05-02 11:43:30 +08:00
|
|
|
PP.Initialize(*Target);
|
2012-11-02 01:52:58 +08:00
|
|
|
InclusionDirectiveCallbacks* Callbacks = new InclusionDirectiveCallbacks;
|
2014-09-10 12:53:53 +08:00
|
|
|
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
|
2012-11-02 01:52:58 +08:00
|
|
|
|
|
|
|
// Lex source text.
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Token Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callbacks have been executed at this point -- return filename range.
|
2018-05-11 03:05:36 +08:00
|
|
|
return Callbacks;
|
2012-11-02 01:52:58 +08:00
|
|
|
}
|
2013-10-12 17:29:48 +08:00
|
|
|
|
2019-01-11 05:22:13 +08:00
|
|
|
std::vector<CondDirectiveCallbacks::Result>
|
|
|
|
DirectiveExprRange(StringRef SourceText) {
|
|
|
|
TrivialModuleLoader ModLoader;
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(SourceText);
|
|
|
|
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
|
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, Target.get());
|
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
2019-03-10 01:33:56 +08:00
|
|
|
SourceMgr, HeaderInfo, ModLoader,
|
2019-01-11 05:22:13 +08:00
|
|
|
/*IILookup =*/nullptr,
|
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
PP.Initialize(*Target);
|
|
|
|
auto *Callbacks = new CondDirectiveCallbacks;
|
|
|
|
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
|
|
|
|
|
|
|
|
// Lex source text.
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Token Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Callbacks->Results;
|
|
|
|
}
|
|
|
|
|
2021-07-03 03:43:01 +08:00
|
|
|
std::vector<PragmaMarkCallbacks::Mark>
|
|
|
|
PragmaMarkCall(const char *SourceText) {
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(SourceText, "test.c");
|
|
|
|
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
|
|
|
|
|
|
|
|
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
|
|
|
|
Diags, LangOpts, Target.get());
|
|
|
|
TrivialModuleLoader ModLoader;
|
|
|
|
|
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
|
|
|
|
SourceMgr, HeaderInfo, ModLoader, /*IILookup=*/nullptr,
|
|
|
|
/*OwnsHeaderSearch=*/false);
|
|
|
|
PP.Initialize(*Target);
|
|
|
|
|
|
|
|
auto *Callbacks = new PragmaMarkCallbacks;
|
|
|
|
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
|
|
|
|
|
|
|
|
// Lex source text.
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
while (true) {
|
|
|
|
Token Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Callbacks->Marks;
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:27:25 +08:00
|
|
|
PragmaOpenCLExtensionCallbacks::CallbackParameters
|
|
|
|
PragmaOpenCLExtensionCall(const char *SourceText) {
|
2013-10-12 17:29:48 +08:00
|
|
|
LangOptions OpenCLLangOpts;
|
|
|
|
OpenCLLangOpts.OpenCL = 1;
|
|
|
|
|
2014-10-16 06:00:40 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(SourceText, "test.cl");
|
2014-08-29 15:59:55 +08:00
|
|
|
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
|
2013-10-12 17:29: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, OpenCLLangOpts, Target.get());
|
2013-10-12 17:29:48 +08:00
|
|
|
|
2017-01-06 03:11:36 +08:00
|
|
|
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags,
|
2019-03-10 01:33:56 +08:00
|
|
|
OpenCLLangOpts, SourceMgr, HeaderInfo, ModLoader,
|
2017-01-06 03:11:36 +08:00
|
|
|
/*IILookup =*/nullptr,
|
2014-05-02 11:43:30 +08:00
|
|
|
/*OwnsHeaderSearch =*/false);
|
|
|
|
PP.Initialize(*Target);
|
2013-10-12 17:29:48 +08:00
|
|
|
|
|
|
|
// parser actually sets correct pragma handlers for preprocessor
|
|
|
|
// according to LangOptions, so we init Parser to register opencl
|
|
|
|
// pragma handlers
|
2018-10-10 21:27:25 +08:00
|
|
|
ASTContext Context(OpenCLLangOpts, SourceMgr, PP.getIdentifierTable(),
|
Reland "[clang-repl] Implement partial translation units and error recovery."
Original commit message:
[clang-repl] Implement partial translation units and error recovery.
https://reviews.llvm.org/D96033 contained a discussion regarding efficient
modeling of error recovery. @rjmccall has outlined the key ideas:
Conceptually, we can split the translation unit into a sequence of partial
translation units (PTUs). Every declaration will be associated with a unique PTU
that owns it.
The first key insight here is that the owning PTU isn't always the "active"
(most recent) PTU, and it isn't always the PTU that the declaration
"comes from". A new declaration (that isn't a redeclaration or specialization of
anything) does belong to the active PTU. A template specialization, however,
belongs to the most recent PTU of all the declarations in its signature - mostly
that means that it can be pulled into a more recent PTU by its template
arguments.
The second key insight is that processing a PTU might extend an earlier PTU.
Rolling back the later PTU shouldn't throw that extension away. For example, if
the second PTU defines a template, and the third PTU requires that template to
be instantiated at float, that template specialization is still part of the
second PTU. Similarly, if the fifth PTU uses an inline function belonging to the
fourth, that definition still belongs to the fourth. When we go to emit code in
a new PTU, we map each declaration we have to emit back to its owning PTU and
emit it in a new module for just the extensions to that PTU. We keep track of
all the modules we've emitted for a PTU so that we can unload them all if we
decide to roll it back.
Most declarations/definitions will only refer to entities from the same or
earlier PTUs. However, it is possible (primarily by defining a
previously-declared entity, but also through templates or ADL) for an entity
that belongs to one PTU to refer to something from a later PTU. We will have to
keep track of this and prevent unwinding to later PTU when we recognize it.
Fortunately, this should be very rare; and crucially, we don't have to do the
bookkeeping for this if we've only got one PTU, e.g. in normal compilation.
Otherwise, PTUs after the first just need to record enough metadata to be able
to revert any changes they've made to declarations belonging to earlier PTUs,
e.g. to redeclaration chains or template specialization lists.
It should even eventually be possible for PTUs to provide their own slab
allocators which can be thrown away as part of rolling back the PTU. We can
maintain a notion of the active allocator and allocate things like Stmt/Expr
nodes in it, temporarily changing it to the appropriate PTU whenever we go to do
something like instantiate a function template. More care will be required when
allocating declarations and types, though.
We would want the PTU to be efficiently recoverable from a Decl; I'm not sure
how best to do that. An easy option that would cover most declarations would be
to make multiple TranslationUnitDecls and parent the declarations appropriately,
but I don't think that's good enough for things like member function templates,
since an instantiation of that would still be parented by its original class.
Maybe we can work this into the DC chain somehow, like how lexical DCs are.
We add a different kind of translation unit `TU_Incremental` which is a
complete translation unit that we might nonetheless incrementally extend later.
Because it is complete (and we might want to generate code for it), we do
perform template instantiation, but because it might be extended later, we don't
warn if it declares or uses undefined internal-linkage symbols.
This patch teaches clang-repl how to recover from errors by disconnecting the
most recent PTU and update the primary PTU lookup tables. For instance:
```./clang-repl
clang-repl> int i = 12; error;
In file included from <<< inputs >>>:1:
input_line_0:1:13: error: C++ requires a type specifier for all declarations
int i = 12; error;
^
error: Parsing failed.
clang-repl> int i = 13; extern "C" int printf(const char*,...);
clang-repl> auto r1 = printf("i=%d\n", i);
i=13
clang-repl> quit
```
Differential revision: https://reviews.llvm.org/D104918
2021-07-12 21:35:20 +08:00
|
|
|
PP.getSelectorTable(), PP.getBuiltinInfo(), PP.TUKind);
|
2014-05-03 11:46:04 +08:00
|
|
|
Context.InitBuiltinTypes(*Target);
|
|
|
|
|
2013-10-12 17:29:48 +08:00
|
|
|
ASTConsumer Consumer;
|
|
|
|
Sema S(PP, Context, Consumer);
|
|
|
|
Parser P(PP, S, false);
|
|
|
|
PragmaOpenCLExtensionCallbacks* Callbacks = new PragmaOpenCLExtensionCallbacks;
|
2014-09-10 12:53:53 +08:00
|
|
|
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
|
2013-10-12 17:29:48 +08:00
|
|
|
|
|
|
|
// Lex source text.
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
while (true) {
|
|
|
|
Token Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.is(tok::eof))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
PragmaOpenCLExtensionCallbacks::CallbackParameters RetVal = {
|
2013-10-14 15:13:59 +08:00
|
|
|
Callbacks->Name,
|
2013-10-12 17:29:48 +08:00
|
|
|
Callbacks->State
|
|
|
|
};
|
2018-10-10 21:27:25 +08:00
|
|
|
return RetVal;
|
2013-10-12 17:29:48 +08:00
|
|
|
}
|
2012-11-02 01:52:58 +08:00
|
|
|
};
|
|
|
|
|
2018-05-11 03:05:36 +08:00
|
|
|
TEST_F(PPCallbacksTest, UserFileCharacteristics) {
|
|
|
|
const char *Source = "#include \"quoted.h\"\n";
|
|
|
|
|
|
|
|
SrcMgr::CharacteristicKind Kind =
|
|
|
|
InclusionDirectiveCharacteristicKind(Source, "/quoted.h", false);
|
|
|
|
|
|
|
|
ASSERT_EQ(SrcMgr::CharacteristicKind::C_User, Kind);
|
|
|
|
}
|
|
|
|
|
2012-11-02 01:52:58 +08:00
|
|
|
TEST_F(PPCallbacksTest, QuotedFilename) {
|
|
|
|
const char* Source =
|
|
|
|
"#include \"quoted.h\"\n";
|
|
|
|
|
|
|
|
CharSourceRange Range =
|
|
|
|
InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
|
|
|
|
|
|
|
|
ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PPCallbacksTest, AngledFilename) {
|
|
|
|
const char* Source =
|
|
|
|
"#include <angled.h>\n";
|
|
|
|
|
|
|
|
CharSourceRange Range =
|
|
|
|
InclusionDirectiveFilenameRange(Source, "/angled.h", true);
|
|
|
|
|
|
|
|
ASSERT_EQ("<angled.h>", GetSourceString(Range));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PPCallbacksTest, QuotedInMacro) {
|
|
|
|
const char* Source =
|
|
|
|
"#define MACRO_QUOTED \"quoted.h\"\n"
|
|
|
|
"#include MACRO_QUOTED\n";
|
|
|
|
|
|
|
|
CharSourceRange Range =
|
|
|
|
InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
|
|
|
|
|
|
|
|
ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PPCallbacksTest, AngledInMacro) {
|
|
|
|
const char* Source =
|
|
|
|
"#define MACRO_ANGLED <angled.h>\n"
|
|
|
|
"#include MACRO_ANGLED\n";
|
|
|
|
|
|
|
|
CharSourceRange Range =
|
|
|
|
InclusionDirectiveFilenameRange(Source, "/angled.h", true);
|
|
|
|
|
|
|
|
ASSERT_EQ("<angled.h>", GetSourceString(Range));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PPCallbacksTest, StringizedMacroArgument) {
|
|
|
|
const char* Source =
|
|
|
|
"#define MACRO_STRINGIZED(x) #x\n"
|
|
|
|
"#include MACRO_STRINGIZED(quoted.h)\n";
|
|
|
|
|
|
|
|
CharSourceRange Range =
|
|
|
|
InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
|
|
|
|
|
|
|
|
ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PPCallbacksTest, ConcatenatedMacroArgument) {
|
|
|
|
const char* Source =
|
|
|
|
"#define MACRO_ANGLED <angled.h>\n"
|
|
|
|
"#define MACRO_CONCAT(x, y) x ## _ ## y\n"
|
|
|
|
"#include MACRO_CONCAT(MACRO, ANGLED)\n";
|
|
|
|
|
|
|
|
CharSourceRange Range =
|
|
|
|
InclusionDirectiveFilenameRange(Source, "/angled.h", false);
|
|
|
|
|
|
|
|
ASSERT_EQ("<angled.h>", GetSourceString(Range));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PPCallbacksTest, TrigraphFilename) {
|
|
|
|
const char* Source =
|
2012-11-04 04:58:26 +08:00
|
|
|
"#include \"tri\?\?-graph.h\"\n";
|
2012-11-02 01:52:58 +08:00
|
|
|
|
|
|
|
CharSourceRange Range =
|
|
|
|
InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
|
|
|
|
|
2012-11-04 04:58:26 +08:00
|
|
|
ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
|
2012-11-02 01:52:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PPCallbacksTest, TrigraphInMacro) {
|
|
|
|
const char* Source =
|
2012-11-04 04:58:26 +08:00
|
|
|
"#define MACRO_TRIGRAPH \"tri\?\?-graph.h\"\n"
|
2012-11-02 01:52:58 +08:00
|
|
|
"#include MACRO_TRIGRAPH\n";
|
|
|
|
|
|
|
|
CharSourceRange Range =
|
|
|
|
InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
|
|
|
|
|
2012-11-04 04:58:26 +08:00
|
|
|
ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
|
2012-11-02 01:52:58 +08:00
|
|
|
}
|
|
|
|
|
2013-10-12 17:29:48 +08:00
|
|
|
TEST_F(PPCallbacksTest, OpenCLExtensionPragmaEnabled) {
|
|
|
|
const char* Source =
|
|
|
|
"#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
|
|
|
|
|
|
|
|
PragmaOpenCLExtensionCallbacks::CallbackParameters Parameters =
|
|
|
|
PragmaOpenCLExtensionCall(Source);
|
|
|
|
|
|
|
|
ASSERT_EQ("cl_khr_fp64", Parameters.Name);
|
|
|
|
unsigned ExpectedState = 1;
|
|
|
|
ASSERT_EQ(ExpectedState, Parameters.State);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PPCallbacksTest, OpenCLExtensionPragmaDisabled) {
|
|
|
|
const char* Source =
|
|
|
|
"#pragma OPENCL EXTENSION cl_khr_fp16 : disable\n";
|
|
|
|
|
|
|
|
PragmaOpenCLExtensionCallbacks::CallbackParameters Parameters =
|
|
|
|
PragmaOpenCLExtensionCall(Source);
|
|
|
|
|
|
|
|
ASSERT_EQ("cl_khr_fp16", Parameters.Name);
|
|
|
|
unsigned ExpectedState = 0;
|
|
|
|
ASSERT_EQ(ExpectedState, Parameters.State);
|
|
|
|
}
|
|
|
|
|
2021-07-03 03:43:01 +08:00
|
|
|
TEST_F(PPCallbacksTest, CollectMarks) {
|
|
|
|
const char *Source =
|
|
|
|
"#pragma mark\n"
|
|
|
|
"#pragma mark\r\n"
|
|
|
|
"#pragma mark - trivia\n"
|
|
|
|
"#pragma mark - trivia\r\n";
|
|
|
|
|
|
|
|
auto Marks = PragmaMarkCall(Source);
|
|
|
|
|
|
|
|
ASSERT_EQ(4u, Marks.size());
|
|
|
|
ASSERT_TRUE(Marks[0].Trivia.empty());
|
|
|
|
ASSERT_TRUE(Marks[1].Trivia.empty());
|
|
|
|
ASSERT_FALSE(Marks[2].Trivia.empty());
|
|
|
|
ASSERT_FALSE(Marks[3].Trivia.empty());
|
|
|
|
ASSERT_EQ(" - trivia", Marks[2].Trivia);
|
|
|
|
ASSERT_EQ(" - trivia", Marks[3].Trivia);
|
|
|
|
}
|
|
|
|
|
2019-01-11 05:22:13 +08:00
|
|
|
TEST_F(PPCallbacksTest, DirectiveExprRanges) {
|
2019-01-18 04:21:34 +08:00
|
|
|
const auto &Results1 = DirectiveExprRange("#if FLUZZY_FLOOF\n#endif\n");
|
2019-01-18 04:52:46 +08:00
|
|
|
EXPECT_EQ(Results1.size(), 1U);
|
2019-01-18 04:21:34 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results1[0].ConditionRange, false)),
|
|
|
|
"FLUZZY_FLOOF");
|
|
|
|
|
|
|
|
const auto &Results2 = DirectiveExprRange("#if 1 + 4 < 7\n#endif\n");
|
2019-01-18 04:52:46 +08:00
|
|
|
EXPECT_EQ(Results2.size(), 1U);
|
2019-01-18 04:21:34 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results2[0].ConditionRange, false)),
|
|
|
|
"1 + 4 < 7");
|
|
|
|
|
|
|
|
const auto &Results3 = DirectiveExprRange("#if 1 + \\\n 2\n#endif\n");
|
2019-01-18 04:52:46 +08:00
|
|
|
EXPECT_EQ(Results3.size(), 1U);
|
2019-01-18 04:21:34 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results3[0].ConditionRange, false)),
|
|
|
|
"1 + \\\n 2");
|
|
|
|
|
|
|
|
const auto &Results4 = DirectiveExprRange("#if 0\n#elif FLOOFY\n#endif\n");
|
2019-01-18 04:52:46 +08:00
|
|
|
EXPECT_EQ(Results4.size(), 2U);
|
2019-01-18 04:21:34 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results4[0].ConditionRange, false)),
|
|
|
|
"0");
|
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results4[1].ConditionRange, false)),
|
|
|
|
"FLOOFY");
|
|
|
|
|
|
|
|
const auto &Results5 = DirectiveExprRange("#if 1\n#elif FLOOFY\n#endif\n");
|
2019-01-18 04:52:46 +08:00
|
|
|
EXPECT_EQ(Results5.size(), 2U);
|
2019-01-18 04:21:34 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results5[0].ConditionRange, false)),
|
|
|
|
"1");
|
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results5[1].ConditionRange, false)),
|
|
|
|
"FLOOFY");
|
|
|
|
|
|
|
|
const auto &Results6 =
|
|
|
|
DirectiveExprRange("#if defined(FLUZZY_FLOOF)\n#endif\n");
|
2019-01-18 04:52:46 +08:00
|
|
|
EXPECT_EQ(Results6.size(), 1U);
|
2019-01-18 04:21:34 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results6[0].ConditionRange, false)),
|
|
|
|
"defined(FLUZZY_FLOOF)");
|
|
|
|
|
|
|
|
const auto &Results7 =
|
|
|
|
DirectiveExprRange("#if 1\n#elif defined(FLOOFY)\n#endif\n");
|
2019-01-18 04:52:46 +08:00
|
|
|
EXPECT_EQ(Results7.size(), 2U);
|
2019-01-18 04:21:34 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results7[0].ConditionRange, false)),
|
|
|
|
"1");
|
|
|
|
EXPECT_EQ(
|
|
|
|
GetSourceStringToEnd(CharSourceRange(Results7[1].ConditionRange, false)),
|
|
|
|
"defined(FLOOFY)");
|
|
|
|
|
2019-01-16 01:20:05 +08:00
|
|
|
const auto &Results8 =
|
|
|
|
DirectiveExprRange("#define FLOOFY 0\n#if __FILE__ > FLOOFY\n#endif\n");
|
|
|
|
EXPECT_EQ(Results8.size(), 1U);
|
2019-01-11 05:22:13 +08:00
|
|
|
EXPECT_EQ(
|
2019-01-16 01:20:05 +08:00
|
|
|
GetSourceStringToEnd(CharSourceRange(Results8[0].ConditionRange, false)),
|
2019-01-18 04:21:34 +08:00
|
|
|
"__FILE__ > FLOOFY");
|
2019-01-11 05:22:13 +08:00
|
|
|
EXPECT_EQ(
|
2019-01-16 01:20:05 +08:00
|
|
|
Lexer::getSourceText(CharSourceRange(Results8[0].ConditionRange, false),
|
|
|
|
SourceMgr, LangOpts),
|
2019-01-18 04:21:34 +08:00
|
|
|
"__FILE__ > FLOOFY");
|
2019-01-11 05:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|