2009-06-20 16:27:14 +08:00
|
|
|
//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// ASTUnit Implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2009-12-01 17:51:01 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-06-20 16:08:23 +08:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/TypeOrdering.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/Basic/TargetOptions.h"
|
2009-12-01 17:51:01 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2009-12-02 11:23:45 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2009-12-01 17:51:01 +08:00
|
|
|
#include "clang/Frontend/FrontendOptions.h"
|
2011-11-28 12:56:00 +08:00
|
|
|
#include "clang/Frontend/MultiplexConsumer.h"
|
2010-10-12 05:37:58 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2009-06-20 16:08:23 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-10-25 01:46:57 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
Remove unnecessary inclusion of Sema.h
Let me tell you a tale...
Within some twisted maze of debug info I've ended up implementing an
insane man's Include What You Use device. When the debugger emits debug
info it really shouldn't, I find out why & then realize the code could
be improved too.
In this instance CIndexDiagnostics.cpp had a lot more debug info with
Clang than GCC. Upon inspection a major culprit was all the debug info
describing clang::Sema. This was emitted because clang::Sema is
befriended by DiagnosticEngine which was rightly required, but GCC
doesn't emit debug info for friends so it never emitted anything for
Clang. Clang does emit debug info for friends (will be fixed/changed to
reduce debug info size).
But why didn't Clang just emit a declaration of Sema if this entire TU
didn't require a definition?
1) Diagnostic.h did the right thing, only using a declaration of Sema
and not including Sema.h at all.
2) Some other dependency of CIndexDiagnostics.cpp didn't do the right
thing. ASTUnit.h, only needing a declaration, still included Sema.h
(hence this commit which removes that include and adds the necessary
includes to the cpp files that were relying on this)
3) -flimit-debug-info didn't save us because of
EnterExpressionEvaluationContext, defined inline in Sema.h which fires
the "requiresCompleteType" check/flag (since it uses nested types from
Sema and calls Sema member functions) and thus, if debug info is ever
emitted for the type, the whole type is emitted and not just a
declaration.
Improving -flimit-debug-info to account for this would be... hard.
Modifying the code so that's not 'required to be complete' might be
possible, but probably only by moving EnterExpressionEvaluationContext
either into Sema, or out of Sema.h. That might be a bit too much of a
contortion to be bothered with.
Also, this is only one of the cases where emitting debug info for
friends caused us to emit a lot more debug info (this change reduces
Clang's DWO size by 0.93%, dropping friends entirely reduces debug info
by 3.2%) - I haven't hunted down the other cases, but I assume they
might be similar (Sema or something like it). IWYU or a similar tool
might help us reduce build times a bit, but analyzing debug info to find
these differences isn't worthwhile. I'll take the 3.2% win, provide this
small improvement to the code itself, and move on.
llvm-svn: 190715
2013-09-14 02:32:52 +08:00
|
|
|
#include "clang/Sema/Sema.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
|
|
|
#include "clang/Serialization/ASTWriter.h"
|
2011-03-23 12:04:01 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2011-02-17 02:16:54 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2010-08-17 07:08:34 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2010-12-07 08:05:48 +08:00
|
|
|
#include "llvm/Support/Atomic.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2011-07-22 02:44:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2011-10-11 05:57:12 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2011-10-28 03:44:25 +08:00
|
|
|
#include "llvm/Support/MutexGuard.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-07-23 10:15:08 +08:00
|
|
|
#include <cstdio>
|
2012-12-04 17:13:33 +08:00
|
|
|
#include <cstdlib>
|
2010-07-31 08:40:00 +08:00
|
|
|
#include <sys/stat.h>
|
2009-06-20 16:08:23 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2010-10-28 23:44:59 +08:00
|
|
|
using llvm::TimeRecord;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class SimpleTimer {
|
|
|
|
bool WantTiming;
|
|
|
|
TimeRecord Start;
|
|
|
|
std::string Output;
|
|
|
|
|
2010-11-10 04:00:56 +08:00
|
|
|
public:
|
2010-11-01 21:48:43 +08:00
|
|
|
explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
|
2010-10-28 23:44:59 +08:00
|
|
|
if (WantTiming)
|
2010-11-10 04:00:56 +08:00
|
|
|
Start = TimeRecord::getCurrentTime();
|
2010-10-28 23:44:59 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void setOutput(const Twine &Output) {
|
2010-10-28 23:44:59 +08:00
|
|
|
if (WantTiming)
|
2010-11-10 04:00:56 +08:00
|
|
|
this->Output = Output.str();
|
2010-10-28 23:44:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~SimpleTimer() {
|
|
|
|
if (WantTiming) {
|
|
|
|
TimeRecord Elapsed = TimeRecord::getCurrentTime();
|
|
|
|
Elapsed -= Start;
|
|
|
|
llvm::errs() << Output << ':';
|
|
|
|
Elapsed.print(Elapsed, llvm::errs());
|
|
|
|
llvm::errs() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-10-28 01:55:18 +08:00
|
|
|
|
|
|
|
struct OnDiskData {
|
|
|
|
/// \brief The file in which the precompiled preamble is stored.
|
|
|
|
std::string PreambleFile;
|
|
|
|
|
2013-06-26 11:52:38 +08:00
|
|
|
/// \brief Temporary files that should be removed when the ASTUnit is
|
2011-10-28 01:55:18 +08:00
|
|
|
/// destroyed.
|
2013-06-26 11:52:38 +08:00
|
|
|
SmallVector<std::string, 4> TemporaryFiles;
|
|
|
|
|
2011-10-28 01:55:18 +08:00
|
|
|
/// \brief Erase temporary files.
|
|
|
|
void CleanTemporaryFiles();
|
|
|
|
|
|
|
|
/// \brief Erase the preamble file.
|
|
|
|
void CleanPreambleFile();
|
|
|
|
|
|
|
|
/// \brief Erase temporary files and the preamble file.
|
|
|
|
void Cleanup();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-10-28 03:44:25 +08:00
|
|
|
static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
|
|
|
|
static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2012-11-15 22:28:07 +08:00
|
|
|
static void cleanupOnDiskMapAtExit();
|
2011-10-28 01:55:18 +08:00
|
|
|
|
|
|
|
typedef llvm::DenseMap<const ASTUnit *, OnDiskData *> OnDiskDataMap;
|
|
|
|
static OnDiskDataMap &getOnDiskDataMap() {
|
|
|
|
static OnDiskDataMap M;
|
|
|
|
static bool hasRegisteredAtExit = false;
|
|
|
|
if (!hasRegisteredAtExit) {
|
|
|
|
hasRegisteredAtExit = true;
|
|
|
|
atexit(cleanupOnDiskMapAtExit);
|
|
|
|
}
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2012-11-15 22:28:07 +08:00
|
|
|
static void cleanupOnDiskMapAtExit() {
|
2012-07-04 00:30:52 +08:00
|
|
|
// Use the mutex because there can be an alive thread destroying an ASTUnit.
|
|
|
|
llvm::MutexGuard Guard(getOnDiskMutex());
|
2011-10-28 01:55:18 +08:00
|
|
|
OnDiskDataMap &M = getOnDiskDataMap();
|
|
|
|
for (OnDiskDataMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
|
|
|
// We don't worry about freeing the memory associated with OnDiskDataMap.
|
|
|
|
// All we care about is erasing stale files.
|
|
|
|
I->second->Cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static OnDiskData &getOnDiskData(const ASTUnit *AU) {
|
2011-10-28 03:44:25 +08:00
|
|
|
// We require the mutex since we are modifying the structure of the
|
|
|
|
// DenseMap.
|
|
|
|
llvm::MutexGuard Guard(getOnDiskMutex());
|
2011-10-28 01:55:18 +08:00
|
|
|
OnDiskDataMap &M = getOnDiskDataMap();
|
|
|
|
OnDiskData *&D = M[AU];
|
|
|
|
if (!D)
|
|
|
|
D = new OnDiskData();
|
|
|
|
return *D;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void erasePreambleFile(const ASTUnit *AU) {
|
|
|
|
getOnDiskData(AU).CleanPreambleFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void removeOnDiskEntry(const ASTUnit *AU) {
|
2011-10-28 03:44:25 +08:00
|
|
|
// We require the mutex since we are modifying the structure of the
|
|
|
|
// DenseMap.
|
|
|
|
llvm::MutexGuard Guard(getOnDiskMutex());
|
2011-10-28 01:55:18 +08:00
|
|
|
OnDiskDataMap &M = getOnDiskDataMap();
|
|
|
|
OnDiskDataMap::iterator I = M.find(AU);
|
|
|
|
if (I != M.end()) {
|
|
|
|
I->second->Cleanup();
|
|
|
|
delete I->second;
|
|
|
|
M.erase(AU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
static void setPreambleFile(const ASTUnit *AU, StringRef preambleFile) {
|
2011-10-28 01:55:18 +08:00
|
|
|
getOnDiskData(AU).PreambleFile = preambleFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const std::string &getPreambleFile(const ASTUnit *AU) {
|
|
|
|
return getOnDiskData(AU).PreambleFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDiskData::CleanTemporaryFiles() {
|
|
|
|
for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
|
2013-06-26 11:52:38 +08:00
|
|
|
llvm::sys::fs::remove(TemporaryFiles[I]);
|
|
|
|
TemporaryFiles.clear();
|
2011-10-28 01:55:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnDiskData::CleanPreambleFile() {
|
|
|
|
if (!PreambleFile.empty()) {
|
2013-06-26 12:02:37 +08:00
|
|
|
llvm::sys::fs::remove(PreambleFile);
|
2011-10-28 01:55:18 +08:00
|
|
|
PreambleFile.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDiskData::Cleanup() {
|
|
|
|
CleanTemporaryFiles();
|
|
|
|
CleanPreambleFile();
|
|
|
|
}
|
|
|
|
|
2012-10-12 00:05:00 +08:00
|
|
|
struct ASTUnit::ASTWriterData {
|
|
|
|
SmallString<128> Buffer;
|
|
|
|
llvm::BitstreamWriter Stream;
|
|
|
|
ASTWriter Writer;
|
|
|
|
|
|
|
|
ASTWriterData() : Stream(Buffer), Writer(Stream) { }
|
|
|
|
};
|
|
|
|
|
2011-10-31 15:19:59 +08:00
|
|
|
void ASTUnit::clearFileLevelDecls() {
|
|
|
|
for (FileDeclsTy::iterator
|
|
|
|
I = FileDecls.begin(), E = FileDecls.end(); I != E; ++I)
|
|
|
|
delete I->second;
|
|
|
|
FileDecls.clear();
|
|
|
|
}
|
|
|
|
|
2011-10-28 01:55:18 +08:00
|
|
|
void ASTUnit::CleanTemporaryFiles() {
|
|
|
|
getOnDiskData(this).CleanTemporaryFiles();
|
|
|
|
}
|
|
|
|
|
2013-06-26 11:52:38 +08:00
|
|
|
void ASTUnit::addTemporaryFile(StringRef TempFile) {
|
2011-10-28 01:55:18 +08:00
|
|
|
getOnDiskData(this).TemporaryFiles.push_back(TempFile);
|
2010-10-28 23:44:59 +08:00
|
|
|
}
|
|
|
|
|
2010-08-04 13:53:38 +08:00
|
|
|
/// \brief After failing to build a precompiled preamble (due to
|
|
|
|
/// errors in the source that occurs in the preamble), the number of
|
|
|
|
/// reparses during which we'll skip even trying to precompile the
|
|
|
|
/// preamble.
|
|
|
|
const unsigned DefaultPreambleRebuildInterval = 5;
|
|
|
|
|
2010-11-17 08:13:31 +08:00
|
|
|
/// \brief Tracks the number of ASTUnit objects that are currently active.
|
|
|
|
///
|
|
|
|
/// Used for debugging purposes only.
|
2010-12-07 08:05:48 +08:00
|
|
|
static llvm::sys::cas_flag ActiveASTUnitObjects;
|
2010-11-17 08:13:31 +08:00
|
|
|
|
2010-04-06 05:10:19 +08:00
|
|
|
ASTUnit::ASTUnit(bool _MainFileIsAST)
|
2013-05-24 13:44:08 +08:00
|
|
|
: Reader(0), HadModuleLoaderFatalFailure(false),
|
|
|
|
OnlyLocalDecls(false), CaptureDiagnostics(false),
|
2011-03-10 01:21:42 +08:00
|
|
|
MainFileIsAST(_MainFileIsAST),
|
2011-08-26 06:30:56 +08:00
|
|
|
TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")),
|
2011-03-05 09:03:48 +08:00
|
|
|
OwnsRemappedFileBuffers(true),
|
2010-10-28 23:44:59 +08:00
|
|
|
NumStoredDiagnosticsFromDriver(0),
|
2010-08-19 09:33:06 +08:00
|
|
|
PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
|
2011-11-30 02:18:33 +08:00
|
|
|
NumWarningsInPreamble(0),
|
2010-08-17 08:40:40 +08:00
|
|
|
ShouldCacheCodeCompletionResults(false),
|
2012-07-12 04:59:04 +08:00
|
|
|
IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
|
2011-02-17 02:16:54 +08:00
|
|
|
CompletionCacheTopLevelHashValue(0),
|
|
|
|
PreambleTopLevelHashValue(0),
|
|
|
|
CurrentTopLevelHashValue(0),
|
2010-08-19 08:45:44 +08:00
|
|
|
UnsafeToFree(false) {
|
2010-11-17 08:13:31 +08:00
|
|
|
if (getenv("LIBCLANG_OBJTRACKING")) {
|
2010-12-07 08:05:48 +08:00
|
|
|
llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
|
2014-02-04 06:20:24 +08:00
|
|
|
fprintf(stderr, "+++ %d translation units\n", (int)ActiveASTUnitObjects);
|
2010-11-17 08:13:31 +08:00
|
|
|
}
|
2010-07-31 04:58:08 +08:00
|
|
|
}
|
2010-04-06 05:10:19 +08:00
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
ASTUnit::~ASTUnit() {
|
2013-05-04 06:58:43 +08:00
|
|
|
// If we loaded from an AST file, balance out the BeginSourceFile call.
|
|
|
|
if (MainFileIsAST && getDiagnostics().getClient()) {
|
|
|
|
getDiagnostics().getClient()->EndSourceFile();
|
|
|
|
}
|
|
|
|
|
2011-10-31 15:19:59 +08:00
|
|
|
clearFileLevelDecls();
|
|
|
|
|
2011-10-28 01:55:18 +08:00
|
|
|
// Clean up the temporary files and the preamble file.
|
|
|
|
removeOnDiskEntry(this);
|
|
|
|
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
// Free the buffers associated with remapped files. We are required to
|
|
|
|
// perform this operation here because we explicitly request that the
|
|
|
|
// compiler instance *not* free these buffers for each invocation of the
|
|
|
|
// parser.
|
2011-03-22 02:40:17 +08:00
|
|
|
if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
|
|
|
|
for (PreprocessorOptions::remapped_file_buffer_iterator
|
|
|
|
FB = PPOpts.remapped_file_buffer_begin(),
|
|
|
|
FBEnd = PPOpts.remapped_file_buffer_end();
|
|
|
|
FB != FBEnd;
|
|
|
|
++FB)
|
|
|
|
delete FB->second;
|
|
|
|
}
|
2010-07-27 22:52:07 +08:00
|
|
|
|
|
|
|
delete SavedMainFileBuffer;
|
2010-08-19 09:33:06 +08:00
|
|
|
delete PreambleBuffer;
|
|
|
|
|
2010-10-28 23:44:59 +08:00
|
|
|
ClearCachedCompletionResults();
|
2010-11-17 08:13:31 +08:00
|
|
|
|
|
|
|
if (getenv("LIBCLANG_OBJTRACKING")) {
|
2010-12-07 08:05:48 +08:00
|
|
|
llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
|
2014-02-04 06:20:24 +08:00
|
|
|
fprintf(stderr, "--- %d translation units\n", (int)ActiveASTUnitObjects);
|
2010-11-17 08:13:31 +08:00
|
|
|
}
|
2010-07-20 05:46:24 +08:00
|
|
|
}
|
|
|
|
|
2012-01-18 02:48:07 +08:00
|
|
|
void ASTUnit::setPreprocessor(Preprocessor *pp) { PP = pp; }
|
|
|
|
|
2010-08-15 14:18:01 +08:00
|
|
|
/// \brief Determine the set of code-completion contexts in which this
|
|
|
|
/// declaration should be shown.
|
2013-01-24 01:21:11 +08:00
|
|
|
static unsigned getDeclShowContexts(const NamedDecl *ND,
|
2010-08-17 07:05:20 +08:00
|
|
|
const LangOptions &LangOpts,
|
|
|
|
bool &IsNestedNameSpecifier) {
|
|
|
|
IsNestedNameSpecifier = false;
|
|
|
|
|
2010-08-15 14:18:01 +08:00
|
|
|
if (isa<UsingShadowDecl>(ND))
|
|
|
|
ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
|
|
|
|
if (!ND)
|
|
|
|
return 0;
|
|
|
|
|
2012-08-14 11:13:00 +08:00
|
|
|
uint64_t Contexts = 0;
|
2010-08-15 14:18:01 +08:00
|
|
|
if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
|
|
|
|
isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
|
|
|
|
// Types can appear in these contexts.
|
|
|
|
if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCIvarList)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ClassStructUnion)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Statement)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Type)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
|
2010-08-15 14:18:01 +08:00
|
|
|
|
|
|
|
// In C++, types can appear in expressions contexts (for functional casts).
|
|
|
|
if (LangOpts.CPlusPlus)
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
|
2010-08-15 14:18:01 +08:00
|
|
|
|
|
|
|
// In Objective-C, message sends can send interfaces. In Objective-C++,
|
|
|
|
// all types are available due to functional casts.
|
|
|
|
if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
|
2011-07-08 00:03:39 +08:00
|
|
|
|
|
|
|
// In Objective-C, you can only be a subclass of another Objective-C class
|
|
|
|
if (isa<ObjCInterfaceDecl>(ND))
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
|
2010-08-15 14:18:01 +08:00
|
|
|
|
|
|
|
// Deal with tag names.
|
|
|
|
if (isa<EnumDecl>(ND)) {
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
|
2010-08-15 14:18:01 +08:00
|
|
|
|
2010-08-17 07:05:20 +08:00
|
|
|
// Part of the nested-name-specifier in C++0x.
|
2013-01-02 19:42:31 +08:00
|
|
|
if (LangOpts.CPlusPlus11)
|
2010-08-17 07:05:20 +08:00
|
|
|
IsNestedNameSpecifier = true;
|
2013-01-24 01:21:11 +08:00
|
|
|
} else if (const RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
|
2010-08-15 14:18:01 +08:00
|
|
|
if (Record->isUnion())
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
|
2010-08-15 14:18:01 +08:00
|
|
|
else
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
|
2010-08-15 14:18:01 +08:00
|
|
|
|
|
|
|
if (LangOpts.CPlusPlus)
|
2010-08-17 07:05:20 +08:00
|
|
|
IsNestedNameSpecifier = true;
|
2010-09-24 07:01:17 +08:00
|
|
|
} else if (isa<ClassTemplateDecl>(ND))
|
2010-08-17 07:05:20 +08:00
|
|
|
IsNestedNameSpecifier = true;
|
2010-08-15 14:18:01 +08:00
|
|
|
} else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
|
|
|
|
// Values can appear in these contexts.
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts = (1LL << CodeCompletionContext::CCC_Statement)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Expression)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
|
2010-08-15 14:18:01 +08:00
|
|
|
} else if (isa<ObjCProtocolDecl>(ND)) {
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
|
2011-07-08 00:03:39 +08:00
|
|
|
} else if (isa<ObjCCategoryDecl>(ND)) {
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
|
2010-08-15 14:18:01 +08:00
|
|
|
} else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
|
2012-08-14 11:13:00 +08:00
|
|
|
Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
|
2010-08-15 14:18:01 +08:00
|
|
|
|
|
|
|
// Part of the nested-name-specifier.
|
2010-08-17 07:05:20 +08:00
|
|
|
IsNestedNameSpecifier = true;
|
2010-08-15 14:18:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Contexts;
|
|
|
|
}
|
|
|
|
|
2010-08-14 06:48:40 +08:00
|
|
|
void ASTUnit::CacheCodeCompletionResults() {
|
|
|
|
if (!TheSema)
|
|
|
|
return;
|
|
|
|
|
2010-10-28 23:44:59 +08:00
|
|
|
SimpleTimer Timer(WantTiming);
|
2010-11-10 04:00:56 +08:00
|
|
|
Timer.setOutput("Cache global code completions for " + getMainFileName());
|
2010-08-14 06:48:40 +08:00
|
|
|
|
|
|
|
// Clear out the previous results.
|
|
|
|
ClearCachedCompletionResults();
|
|
|
|
|
|
|
|
// Gather the set of global code completions.
|
2010-08-25 14:19:51 +08:00
|
|
|
typedef CodeCompletionResult Result;
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Result, 8> Results;
|
2011-02-17 03:08:06 +08:00
|
|
|
CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
|
2012-11-16 11:34:57 +08:00
|
|
|
CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
|
2012-04-11 01:23:48 +08:00
|
|
|
TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
|
2012-11-16 11:34:57 +08:00
|
|
|
CCTUInfo, Results);
|
2010-08-14 06:48:40 +08:00
|
|
|
|
|
|
|
// Translate global code completions into cached completions.
|
2010-08-17 02:08:11 +08:00
|
|
|
llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
|
|
|
|
|
2010-08-14 06:48:40 +08:00
|
|
|
for (unsigned I = 0, N = Results.size(); I != N; ++I) {
|
|
|
|
switch (Results[I].Kind) {
|
2010-08-15 14:18:01 +08:00
|
|
|
case Result::RK_Declaration: {
|
2010-08-17 07:05:20 +08:00
|
|
|
bool IsNestedNameSpecifier = false;
|
2010-08-15 14:18:01 +08:00
|
|
|
CachedCodeCompletionResult CachedResult;
|
2011-02-02 03:23:04 +08:00
|
|
|
CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
|
2012-04-11 01:23:48 +08:00
|
|
|
*CachedCompletionAllocator,
|
2012-11-16 11:34:57 +08:00
|
|
|
CCTUInfo,
|
2012-07-03 01:35:10 +08:00
|
|
|
IncludeBriefCommentsInCodeCompletion);
|
2010-08-15 14:18:01 +08:00
|
|
|
CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
|
2012-03-11 15:00:24 +08:00
|
|
|
Ctx->getLangOpts(),
|
2010-08-17 07:05:20 +08:00
|
|
|
IsNestedNameSpecifier);
|
2010-08-15 14:18:01 +08:00
|
|
|
CachedResult.Priority = Results[I].Priority;
|
|
|
|
CachedResult.Kind = Results[I].CursorKind;
|
2010-08-24 07:00:57 +08:00
|
|
|
CachedResult.Availability = Results[I].Availability;
|
2010-08-17 00:46:30 +08:00
|
|
|
|
2010-08-17 02:08:11 +08:00
|
|
|
// Keep track of the type of this completion in an ASTContext-agnostic
|
|
|
|
// way.
|
2010-08-17 00:46:30 +08:00
|
|
|
QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
|
2010-08-17 02:08:11 +08:00
|
|
|
if (UsageType.isNull()) {
|
2010-08-17 00:46:30 +08:00
|
|
|
CachedResult.TypeClass = STC_Void;
|
2010-08-17 02:08:11 +08:00
|
|
|
CachedResult.Type = 0;
|
|
|
|
} else {
|
|
|
|
CanQualType CanUsageType
|
|
|
|
= Ctx->getCanonicalType(UsageType.getUnqualifiedType());
|
|
|
|
CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
|
|
|
|
|
|
|
|
// Determine whether we have already seen this type. If so, we save
|
|
|
|
// ourselves the work of formatting the type string by using the
|
|
|
|
// temporary, CanQualType-based hash table to find the associated value.
|
|
|
|
unsigned &TypeValue = CompletionTypes[CanUsageType];
|
|
|
|
if (TypeValue == 0) {
|
|
|
|
TypeValue = CompletionTypes.size();
|
|
|
|
CachedCompletionTypes[QualType(CanUsageType).getAsString()]
|
|
|
|
= TypeValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedResult.Type = TypeValue;
|
2010-08-17 00:46:30 +08:00
|
|
|
}
|
2010-08-17 02:08:11 +08:00
|
|
|
|
2010-08-15 14:18:01 +08:00
|
|
|
CachedCompletionResults.push_back(CachedResult);
|
2010-08-17 07:05:20 +08:00
|
|
|
|
|
|
|
/// Handle nested-name-specifiers in C++.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (TheSema->Context.getLangOpts().CPlusPlus &&
|
2010-08-17 07:05:20 +08:00
|
|
|
IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
|
|
|
|
// The contexts in which a nested-name-specifier can appear in C++.
|
2012-08-14 11:13:00 +08:00
|
|
|
uint64_t NNSContexts
|
|
|
|
= (1LL << CodeCompletionContext::CCC_TopLevel)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCIvarList)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ClassStructUnion)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Statement)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Expression)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_EnumTag)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_UnionTag)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Type)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
|
2010-08-17 07:05:20 +08:00
|
|
|
|
|
|
|
if (isa<NamespaceDecl>(Results[I].Declaration) ||
|
|
|
|
isa<NamespaceAliasDecl>(Results[I].Declaration))
|
2012-08-14 11:13:00 +08:00
|
|
|
NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
|
2010-08-17 07:05:20 +08:00
|
|
|
|
|
|
|
if (unsigned RemainingContexts
|
|
|
|
= NNSContexts & ~CachedResult.ShowInContexts) {
|
|
|
|
// If there any contexts where this completion can be a
|
|
|
|
// nested-name-specifier but isn't already an option, create a
|
|
|
|
// nested-name-specifier completion.
|
|
|
|
Results[I].StartsNestedNameSpecifier = true;
|
2011-02-02 03:23:04 +08:00
|
|
|
CachedResult.Completion
|
|
|
|
= Results[I].CreateCodeCompletionString(*TheSema,
|
2012-04-11 01:23:48 +08:00
|
|
|
*CachedCompletionAllocator,
|
2012-11-16 11:34:57 +08:00
|
|
|
CCTUInfo,
|
2012-07-03 01:35:10 +08:00
|
|
|
IncludeBriefCommentsInCodeCompletion);
|
2010-08-17 07:05:20 +08:00
|
|
|
CachedResult.ShowInContexts = RemainingContexts;
|
|
|
|
CachedResult.Priority = CCP_NestedNameSpecifier;
|
|
|
|
CachedResult.TypeClass = STC_Void;
|
|
|
|
CachedResult.Type = 0;
|
|
|
|
CachedCompletionResults.push_back(CachedResult);
|
|
|
|
}
|
|
|
|
}
|
2010-08-14 06:48:40 +08:00
|
|
|
break;
|
2010-08-15 14:18:01 +08:00
|
|
|
}
|
|
|
|
|
2010-08-14 06:48:40 +08:00
|
|
|
case Result::RK_Keyword:
|
|
|
|
case Result::RK_Pattern:
|
|
|
|
// Ignore keywords and patterns; we don't care, since they are so
|
|
|
|
// easily regenerated.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Result::RK_Macro: {
|
|
|
|
CachedCodeCompletionResult CachedResult;
|
2011-02-02 03:23:04 +08:00
|
|
|
CachedResult.Completion
|
|
|
|
= Results[I].CreateCodeCompletionString(*TheSema,
|
2012-04-11 01:23:48 +08:00
|
|
|
*CachedCompletionAllocator,
|
2012-11-16 11:34:57 +08:00
|
|
|
CCTUInfo,
|
2012-07-03 01:35:10 +08:00
|
|
|
IncludeBriefCommentsInCodeCompletion);
|
2010-08-14 06:48:40 +08:00
|
|
|
CachedResult.ShowInContexts
|
2012-08-14 11:13:00 +08:00
|
|
|
= (1LL << CodeCompletionContext::CCC_TopLevel)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCInterface)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCImplementation)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCIvarList)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ClassStructUnion)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Statement)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Expression)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_MacroNameUse)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_OtherWithMacros);
|
2010-08-24 02:23:48 +08:00
|
|
|
|
2010-08-14 06:48:40 +08:00
|
|
|
CachedResult.Priority = Results[I].Priority;
|
|
|
|
CachedResult.Kind = Results[I].CursorKind;
|
2010-08-24 07:00:57 +08:00
|
|
|
CachedResult.Availability = Results[I].Availability;
|
2010-08-17 00:18:59 +08:00
|
|
|
CachedResult.TypeClass = STC_Void;
|
2010-08-17 02:08:11 +08:00
|
|
|
CachedResult.Type = 0;
|
2010-08-14 06:48:40 +08:00
|
|
|
CachedCompletionResults.push_back(CachedResult);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-17 02:16:54 +08:00
|
|
|
|
|
|
|
// Save the current top-level hash value.
|
|
|
|
CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
|
2010-08-14 06:48:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTUnit::ClearCachedCompletionResults() {
|
|
|
|
CachedCompletionResults.clear();
|
2010-08-17 02:08:11 +08:00
|
|
|
CachedCompletionTypes.clear();
|
2011-02-17 03:08:06 +08:00
|
|
|
CachedCompletionAllocator = 0;
|
2010-08-14 06:48:40 +08:00
|
|
|
}
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
namespace {
|
|
|
|
|
2010-08-19 07:56:43 +08:00
|
|
|
/// \brief Gathers information from ASTReader that will be used to initialize
|
2009-06-20 16:08:23 +08:00
|
|
|
/// a Preprocessor.
|
2010-08-19 07:57:06 +08:00
|
|
|
class ASTInfoCollector : public ASTReaderListener {
|
2011-09-02 07:39:15 +08:00
|
|
|
Preprocessor &PP;
|
2011-09-02 08:18:52 +08:00
|
|
|
ASTContext &Context;
|
2009-06-20 16:08:23 +08:00
|
|
|
LangOptions &LangOpt;
|
2012-10-17 07:40:58 +08:00
|
|
|
IntrusiveRefCntPtr<TargetOptions> &TargetOpts;
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<TargetInfo> &Target;
|
2009-06-20 16:08:23 +08:00
|
|
|
unsigned &Counter;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-02 08:18:52 +08:00
|
|
|
bool InitializedLanguage;
|
2009-06-20 16:08:23 +08:00
|
|
|
public:
|
2011-09-02 08:18:52 +08:00
|
|
|
ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
|
2012-10-17 07:40:58 +08:00
|
|
|
IntrusiveRefCntPtr<TargetOptions> &TargetOpts,
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<TargetInfo> &Target,
|
2009-06-20 16:08:23 +08:00
|
|
|
unsigned &Counter)
|
2013-05-09 07:46:55 +08:00
|
|
|
: PP(PP), Context(Context), LangOpt(LangOpt),
|
2012-10-16 00:45:32 +08:00
|
|
|
TargetOpts(TargetOpts), Target(Target),
|
2013-05-09 07:46:55 +08:00
|
|
|
Counter(Counter),
|
2011-09-02 08:18:52 +08:00
|
|
|
InitializedLanguage(false) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-10-23 14:18:24 +08:00
|
|
|
virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
|
2012-10-23 07:51:00 +08:00
|
|
|
bool Complain) {
|
2012-10-12 00:05:00 +08:00
|
|
|
if (InitializedLanguage)
|
2011-09-02 07:39:15 +08:00
|
|
|
return false;
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
LangOpt = LangOpts;
|
2011-09-02 08:18:52 +08:00
|
|
|
InitializedLanguage = true;
|
2012-10-12 00:05:00 +08:00
|
|
|
|
|
|
|
updated();
|
2009-06-20 16:08:23 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-10-23 14:18:24 +08:00
|
|
|
virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
|
2012-10-23 07:51:00 +08:00
|
|
|
bool Complain) {
|
2011-09-02 07:39:15 +08:00
|
|
|
// If we've already initialized the target, don't do it again.
|
2012-10-12 00:05:00 +08:00
|
|
|
if (Target)
|
2011-09-02 07:39:15 +08:00
|
|
|
return false;
|
|
|
|
|
2012-10-17 07:40:58 +08:00
|
|
|
this->TargetOpts = new TargetOptions(TargetOpts);
|
2012-11-16 12:24:59 +08:00
|
|
|
Target = TargetInfo::CreateTargetInfo(PP.getDiagnostics(),
|
|
|
|
&*this->TargetOpts);
|
2012-09-15 04:24:53 +08:00
|
|
|
|
2012-10-12 00:05:00 +08:00
|
|
|
updated();
|
2009-06-20 16:08:23 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-10-10 10:12:47 +08:00
|
|
|
virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value) {
|
2009-06-20 16:08:23 +08:00
|
|
|
Counter = Value;
|
|
|
|
}
|
2012-09-15 04:24:53 +08:00
|
|
|
|
|
|
|
private:
|
2012-10-12 00:05:00 +08:00
|
|
|
void updated() {
|
|
|
|
if (!Target || !InitializedLanguage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Inform the target of the language options.
|
|
|
|
//
|
|
|
|
// FIXME: We shouldn't need to do this, the target should be immutable once
|
|
|
|
// created. This complexity should be lifted elsewhere.
|
|
|
|
Target->setForcedLangOptions(LangOpt);
|
|
|
|
|
|
|
|
// Initialize the preprocessor.
|
|
|
|
PP.Initialize(*Target);
|
|
|
|
|
|
|
|
// Initialize the ASTContext
|
|
|
|
Context.InitBuiltinTypes(*Target);
|
2013-02-22 22:21:27 +08:00
|
|
|
|
|
|
|
// We didn't have access to the comment options when the ASTContext was
|
|
|
|
// constructed, so register them now.
|
|
|
|
Context.getCommentCommandTraits().registerCommentOptions(
|
|
|
|
LangOpt.CommentOpts);
|
2012-09-15 04:24:53 +08:00
|
|
|
}
|
2009-06-20 16:08:23 +08:00
|
|
|
};
|
|
|
|
|
2013-05-04 06:58:43 +08:00
|
|
|
/// \brief Diagnostic consumer that saves each diagnostic it is given.
|
2011-09-26 08:01:39 +08:00
|
|
|
class StoredDiagnosticConsumer : public DiagnosticConsumer {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<StoredDiagnostic> &StoredDiags;
|
2013-05-04 06:58:43 +08:00
|
|
|
SourceManager *SourceMgr;
|
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
public:
|
2011-09-26 08:01:39 +08:00
|
|
|
explicit StoredDiagnosticConsumer(
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<StoredDiagnostic> &StoredDiags)
|
2013-05-04 06:58:43 +08:00
|
|
|
: StoredDiags(StoredDiags), SourceMgr(0) { }
|
|
|
|
|
|
|
|
virtual void BeginSourceFile(const LangOptions &LangOpts,
|
|
|
|
const Preprocessor *PP = 0) {
|
|
|
|
if (PP)
|
|
|
|
SourceMgr = &PP->getSourceManager();
|
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
|
2011-09-26 09:18:08 +08:00
|
|
|
const Diagnostic &Info);
|
2010-02-19 02:08:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief RAII object that optionally captures diagnostics, if
|
|
|
|
/// there is no diagnostic client to capture them already.
|
|
|
|
class CaptureDroppedDiagnostics {
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags;
|
2011-09-26 08:01:39 +08:00
|
|
|
StoredDiagnosticConsumer Client;
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *PreviousClient;
|
2010-02-19 02:08:43 +08:00
|
|
|
|
|
|
|
public:
|
2011-09-26 07:23:43 +08:00
|
|
|
CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<StoredDiagnostic> &StoredDiags)
|
2010-08-19 06:29:43 +08:00
|
|
|
: Diags(Diags), Client(StoredDiags), PreviousClient(0)
|
2010-02-19 02:08:43 +08:00
|
|
|
{
|
2010-08-19 06:29:43 +08:00
|
|
|
if (RequestCapture || Diags.getClient() == 0) {
|
|
|
|
PreviousClient = Diags.takeClient();
|
2010-02-19 02:08:43 +08:00
|
|
|
Diags.setClient(&Client);
|
2010-08-19 06:29:43 +08:00
|
|
|
}
|
2010-02-19 02:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~CaptureDroppedDiagnostics() {
|
2010-08-19 06:29:43 +08:00
|
|
|
if (Diags.getClient() == &Client) {
|
|
|
|
Diags.takeClient();
|
|
|
|
Diags.setClient(PreviousClient);
|
|
|
|
}
|
2010-02-19 02:08:43 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-09-26 08:01:39 +08:00
|
|
|
void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
|
2011-09-26 09:18:08 +08:00
|
|
|
const Diagnostic &Info) {
|
2010-11-19 04:06:46 +08:00
|
|
|
// Default implementation (Warnings/errors count).
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer::HandleDiagnostic(Level, Info);
|
2010-11-19 04:06:46 +08:00
|
|
|
|
2013-05-04 06:58:43 +08:00
|
|
|
// Only record the diagnostic if it's part of the source manager we know
|
|
|
|
// about. This effectively drops diagnostics from modules we're building.
|
|
|
|
// FIXME: In the long run, ee don't want to drop source managers from modules.
|
|
|
|
if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr)
|
|
|
|
StoredDiags.push_back(StoredDiagnostic(Level, Info));
|
2010-02-19 02:08:43 +08:00
|
|
|
}
|
|
|
|
|
2013-05-10 09:28:51 +08:00
|
|
|
ASTMutationListener *ASTUnit::getASTMutationListener() {
|
|
|
|
if (WriterData)
|
|
|
|
return &WriterData->Writer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-12 00:05:00 +08:00
|
|
|
ASTDeserializationListener *ASTUnit::getDeserializationListener() {
|
|
|
|
if (WriterData)
|
|
|
|
return &WriterData->Writer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename,
|
2010-11-23 17:19:42 +08:00
|
|
|
std::string *ErrorStr) {
|
2010-11-23 16:35:12 +08:00
|
|
|
assert(FileMgr);
|
2010-11-23 17:19:42 +08:00
|
|
|
return FileMgr->getBufferForFile(Filename, ErrorStr);
|
2010-11-04 06:45:23 +08:00
|
|
|
}
|
|
|
|
|
2010-11-11 08:39:14 +08:00
|
|
|
/// \brief Configure the diagnostics object for use with ASTUnit.
|
2012-02-20 22:00:23 +08:00
|
|
|
void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
|
2011-01-19 09:02:47 +08:00
|
|
|
const char **ArgBegin, const char **ArgEnd,
|
2010-11-11 08:39:14 +08:00
|
|
|
ASTUnit &AST, bool CaptureDiagnostics) {
|
|
|
|
if (!Diags.getPtr()) {
|
|
|
|
// No diagnostics engine was provided, so create our own diagnostics object
|
|
|
|
// with the default options.
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *Client = 0;
|
2010-11-11 08:39:14 +08:00
|
|
|
if (CaptureDiagnostics)
|
2011-09-26 08:01:39 +08:00
|
|
|
Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics);
|
2012-10-24 06:26:28 +08:00
|
|
|
Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions(),
|
2013-01-20 09:58:28 +08:00
|
|
|
Client,
|
2013-05-04 07:07:45 +08:00
|
|
|
/*ShouldOwnClient=*/true);
|
2010-11-11 08:39:14 +08:00
|
|
|
} else if (CaptureDiagnostics) {
|
2011-09-26 08:01:39 +08:00
|
|
|
Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics));
|
2010-11-11 08:39:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-19 07:57:06 +08:00
|
|
|
ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
|
2010-11-04 06:45:23 +08:00
|
|
|
const FileSystemOptions &FileSystemOpts,
|
2009-10-17 08:34:24 +08:00
|
|
|
bool OnlyLocalDecls,
|
2014-02-07 23:00:22 +08:00
|
|
|
ArrayRef<RemappedFile> RemappedFiles,
|
2012-03-07 09:51:17 +08:00
|
|
|
bool CaptureDiagnostics,
|
2012-07-12 04:59:04 +08:00
|
|
|
bool AllowPCHWithCompilerErrors,
|
|
|
|
bool UserFilesAreVolatile) {
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ASTUnit> AST(new ASTUnit(true));
|
2011-03-18 10:06:56 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2011-03-22 09:15:24 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
|
|
|
|
ASTUnitCleanup(AST.get());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
|
|
|
|
llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
|
2011-03-22 09:15:24 +08:00
|
|
|
DiagCleanup(Diags.getPtr());
|
2011-03-18 10:06:56 +08:00
|
|
|
|
2011-01-19 09:02:47 +08:00
|
|
|
ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2009-10-17 04:01:17 +08:00
|
|
|
AST->OnlyLocalDecls = OnlyLocalDecls;
|
2010-11-11 08:39:14 +08:00
|
|
|
AST->CaptureDiagnostics = CaptureDiagnostics;
|
2010-04-06 07:52:57 +08:00
|
|
|
AST->Diagnostics = Diags;
|
2011-03-22 02:40:17 +08:00
|
|
|
AST->FileMgr = new FileManager(FileSystemOpts);
|
2012-07-12 04:59:04 +08:00
|
|
|
AST->UserFilesAreVolatile = UserFilesAreVolatile;
|
2011-03-22 02:40:17 +08:00
|
|
|
AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
|
2012-07-12 04:59:04 +08:00
|
|
|
AST->getFileManager(),
|
|
|
|
UserFilesAreVolatile);
|
2012-10-25 00:19:39 +08:00
|
|
|
AST->HSOpts = new HeaderSearchOptions();
|
|
|
|
|
|
|
|
AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
|
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
|
|
|
AST->getSourceManager(),
|
2011-12-31 12:05:44 +08:00
|
|
|
AST->getDiagnostics(),
|
2012-01-30 14:01:29 +08:00
|
|
|
AST->ASTFileLangOpts,
|
|
|
|
/*Target=*/0));
|
2011-03-05 09:03:53 +08:00
|
|
|
|
2014-02-08 08:38:15 +08:00
|
|
|
for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
|
|
|
|
const llvm::MemoryBuffer *MemBuf = RemappedFiles[I].second;
|
|
|
|
// Create the file entry for the file that we're mapping from.
|
|
|
|
const FileEntry *FromFile = AST->getFileManager().getVirtualFile(
|
|
|
|
RemappedFiles[I].first, MemBuf->getBufferSize(), 0);
|
|
|
|
if (!FromFile) {
|
|
|
|
AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
|
2011-03-05 09:03:53 +08:00
|
|
|
<< RemappedFiles[I].first;
|
2014-02-08 08:38:15 +08:00
|
|
|
delete MemBuf;
|
|
|
|
continue;
|
2010-01-23 08:14:00 +08:00
|
|
|
}
|
2014-02-08 08:38:15 +08:00
|
|
|
|
|
|
|
// Override the contents of the "from" file with the contents of
|
|
|
|
// the "to" file.
|
|
|
|
AST->getSourceManager().overrideFileContents(FromFile, MemBuf);
|
2010-01-23 08:14:00 +08:00
|
|
|
}
|
2014-02-08 08:38:15 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// Gather Info for preprocessor construction later on.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
|
|
|
|
unsigned Counter;
|
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ASTReader> Reader;
|
2009-06-20 16:08:23 +08:00
|
|
|
|
2012-10-25 01:46:57 +08:00
|
|
|
AST->PP = new Preprocessor(new PreprocessorOptions(),
|
|
|
|
AST->getDiagnostics(), AST->ASTFileLangOpts,
|
2011-09-02 07:39:15 +08:00
|
|
|
/*Target=*/0, AST->getSourceManager(), HeaderInfo,
|
|
|
|
*AST,
|
|
|
|
/*IILookup=*/0,
|
|
|
|
/*OwnsHeaderSearch=*/false,
|
|
|
|
/*DelayInitialization=*/true);
|
2011-09-02 08:18:52 +08:00
|
|
|
Preprocessor &PP = *AST->PP;
|
|
|
|
|
|
|
|
AST->Ctx = new ASTContext(AST->ASTFileLangOpts,
|
|
|
|
AST->getSourceManager(),
|
|
|
|
/*Target=*/0,
|
|
|
|
PP.getIdentifierTable(),
|
|
|
|
PP.getSelectorTable(),
|
|
|
|
PP.getBuiltinInfo(),
|
|
|
|
/* size_reserve = */0,
|
|
|
|
/*DelayInitialization=*/true);
|
|
|
|
ASTContext &Context = *AST->Ctx;
|
2011-09-02 07:39:15 +08:00
|
|
|
|
2012-09-15 09:10:20 +08:00
|
|
|
bool disableValid = false;
|
|
|
|
if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
|
|
|
|
disableValid = true;
|
2012-03-07 09:51:17 +08:00
|
|
|
Reader.reset(new ASTReader(PP, Context,
|
|
|
|
/*isysroot=*/"",
|
2012-09-15 09:10:20 +08:00
|
|
|
/*DisableValidation=*/disableValid,
|
2012-03-07 09:51:17 +08:00
|
|
|
AllowPCHWithCompilerErrors));
|
2011-05-05 07:27:12 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<ASTReader>
|
|
|
|
ReaderCleanup(Reader.get());
|
|
|
|
|
2011-09-02 08:18:52 +08:00
|
|
|
Reader->setListener(new ASTInfoCollector(*AST->PP, Context,
|
2013-05-09 07:46:55 +08:00
|
|
|
AST->ASTFileLangOpts,
|
2012-10-16 00:45:32 +08:00
|
|
|
AST->TargetOpts, AST->Target,
|
2012-10-25 08:09:28 +08:00
|
|
|
Counter));
|
2009-09-03 13:59:35 +08:00
|
|
|
|
2012-10-23 07:51:00 +08:00
|
|
|
switch (Reader->ReadAST(Filename, serialization::MK_MainFile,
|
2012-11-16 02:57:22 +08:00
|
|
|
SourceLocation(), ASTReader::ARR_None)) {
|
2010-08-19 07:56:43 +08:00
|
|
|
case ASTReader::Success:
|
2009-06-20 16:08:23 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-19 07:56:43 +08:00
|
|
|
case ASTReader::Failure:
|
2013-03-19 08:28:20 +08:00
|
|
|
case ASTReader::Missing:
|
2012-10-23 06:50:17 +08:00
|
|
|
case ASTReader::OutOfDate:
|
|
|
|
case ASTReader::VersionMismatch:
|
|
|
|
case ASTReader::ConfigurationMismatch:
|
|
|
|
case ASTReader::HadErrors:
|
2010-04-06 05:10:19 +08:00
|
|
|
AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
|
2009-06-20 16:08:23 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-02 16:44:16 +08:00
|
|
|
AST->OriginalSourceFile = Reader->getOriginalSourceFile();
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
PP.setCounterValue(Counter);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-19 07:56:43 +08:00
|
|
|
// Attach the AST reader to the AST context as an external AST
|
2009-06-20 16:08:23 +08:00
|
|
|
// source, so that declarations will be deserialized from the
|
2010-08-19 07:57:06 +08:00
|
|
|
// AST file as needed.
|
2010-08-19 07:56:43 +08:00
|
|
|
ASTReader *ReaderPtr = Reader.get();
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ExternalASTSource> Source(Reader.take());
|
2011-05-05 07:27:12 +08:00
|
|
|
|
|
|
|
// Unregister the cleanup for ASTReader. It will get cleaned up
|
|
|
|
// by the ASTUnit cleanup.
|
|
|
|
ReaderCleanup.unregister();
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
Context.setExternalSource(Source);
|
|
|
|
|
2010-08-13 11:15:25 +08:00
|
|
|
// Create an AST consumer, even though it isn't used.
|
|
|
|
AST->Consumer.reset(new ASTConsumer);
|
|
|
|
|
2010-08-19 07:56:43 +08:00
|
|
|
// Create a semantic analysis object and tell the AST reader about it.
|
2010-08-13 11:15:25 +08:00
|
|
|
AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
|
|
|
|
AST->TheSema->Initialize();
|
|
|
|
ReaderPtr->InitializeSema(*AST->TheSema);
|
2011-11-02 01:14:15 +08:00
|
|
|
AST->Reader = ReaderPtr;
|
2010-08-13 11:15:25 +08:00
|
|
|
|
2013-05-04 06:58:43 +08:00
|
|
|
// Tell the diagnostic client that we have started a source file.
|
|
|
|
AST->getDiagnostics().getClient()->BeginSourceFile(Context.getLangOpts(),&PP);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return AST.take();
|
2009-06-20 16:08:23 +08:00
|
|
|
}
|
2009-12-01 17:51:01 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-02-17 02:16:54 +08:00
|
|
|
/// \brief Preprocessor callback class that updates a hash value with the names
|
|
|
|
/// of all macros that have been defined by the translation unit.
|
|
|
|
class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
|
|
|
|
unsigned &Hash;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
|
|
|
|
|
2013-02-24 08:05:14 +08:00
|
|
|
virtual void MacroDefined(const Token &MacroNameTok,
|
|
|
|
const MacroDirective *MD) {
|
2011-02-17 02:16:54 +08:00
|
|
|
Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Add the given declaration to the hash of all top-level entities.
|
|
|
|
void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
|
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DeclContext *DC = D->getDeclContext();
|
|
|
|
if (!DC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
|
2013-10-16 01:37:55 +08:00
|
|
|
if (EnumDecl *EnumD = dyn_cast<EnumDecl>(D)) {
|
|
|
|
// For an unscoped enum include the enumerators in the hash since they
|
|
|
|
// enter the top-level namespace.
|
|
|
|
if (!EnumD->isScoped()) {
|
|
|
|
for (EnumDecl::enumerator_iterator EI = EnumD->enumerator_begin(),
|
|
|
|
EE = EnumD->enumerator_end(); EI != EE; ++EI) {
|
|
|
|
if ((*EI)->getIdentifier())
|
|
|
|
Hash = llvm::HashString((*EI)->getIdentifier()->getName(), Hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-17 02:16:54 +08:00
|
|
|
if (ND->getIdentifier())
|
|
|
|
Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
|
|
|
|
else if (DeclarationName Name = ND->getDeclName()) {
|
|
|
|
std::string NameStr = Name.getAsString();
|
|
|
|
Hash = llvm::HashString(NameStr, Hash);
|
|
|
|
}
|
|
|
|
return;
|
2013-06-25 05:19:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) {
|
|
|
|
if (Module *Mod = ImportD->getImportedModule()) {
|
|
|
|
std::string ModName = Mod->getFullModuleName();
|
|
|
|
Hash = llvm::HashString(ModName, Hash);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-02-17 02:16:54 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 16:17:33 +08:00
|
|
|
class TopLevelDeclTrackerConsumer : public ASTConsumer {
|
|
|
|
ASTUnit &Unit;
|
2011-02-17 02:16:54 +08:00
|
|
|
unsigned &Hash;
|
|
|
|
|
2009-12-04 16:17:33 +08:00
|
|
|
public:
|
2011-02-17 02:16:54 +08:00
|
|
|
TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
|
|
|
|
: Unit(_Unit), Hash(Hash) {
|
|
|
|
Hash = 0;
|
|
|
|
}
|
|
|
|
|
2011-10-31 15:19:59 +08:00
|
|
|
void handleTopLevelDecl(Decl *D) {
|
2011-11-16 10:35:10 +08:00
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
|
2011-10-31 15:19:59 +08:00
|
|
|
// FIXME: Currently ObjC method declarations are incorrectly being
|
|
|
|
// reported as top-level declarations, even though their DeclContext
|
|
|
|
// is the containing ObjC @interface/@implementation. This is a
|
|
|
|
// fundamental problem in the parser right now.
|
|
|
|
if (isa<ObjCMethodDecl>(D))
|
|
|
|
return;
|
|
|
|
|
|
|
|
AddTopLevelDeclarationToHash(D, Hash);
|
|
|
|
Unit.addTopLevelDecl(D);
|
|
|
|
|
|
|
|
handleFileLevelDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleFileLevelDecl(Decl *D) {
|
|
|
|
Unit.addFileLevelDecl(D);
|
|
|
|
if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) {
|
|
|
|
for (NamespaceDecl::decl_iterator
|
|
|
|
I = NSD->decls_begin(), E = NSD->decls_end(); I != E; ++I)
|
|
|
|
handleFileLevelDecl(*I);
|
2010-05-04 04:16:35 +08:00
|
|
|
}
|
2009-12-04 16:17:33 +08:00
|
|
|
}
|
2010-08-12 02:52:41 +08:00
|
|
|
|
2011-11-18 08:26:59 +08:00
|
|
|
bool HandleTopLevelDecl(DeclGroupRef D) {
|
2011-10-31 15:19:59 +08:00
|
|
|
for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
|
|
|
|
handleTopLevelDecl(*it);
|
2011-11-18 08:26:59 +08:00
|
|
|
return true;
|
2011-10-31 15:19:59 +08:00
|
|
|
}
|
|
|
|
|
2010-08-12 02:52:41 +08:00
|
|
|
// We're not interested in "interesting" decls.
|
|
|
|
void HandleInterestingDecl(DeclGroupRef) {}
|
2011-10-31 15:19:59 +08:00
|
|
|
|
|
|
|
void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
|
|
|
|
for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
|
|
|
|
handleTopLevelDecl(*it);
|
|
|
|
}
|
2012-10-12 00:05:00 +08:00
|
|
|
|
2013-05-10 09:28:51 +08:00
|
|
|
virtual ASTMutationListener *GetASTMutationListener() {
|
|
|
|
return Unit.getASTMutationListener();
|
|
|
|
}
|
|
|
|
|
2012-10-12 00:05:00 +08:00
|
|
|
virtual ASTDeserializationListener *GetASTDeserializationListener() {
|
|
|
|
return Unit.getDeserializationListener();
|
|
|
|
}
|
2009-12-04 16:17:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class TopLevelDeclTrackerAction : public ASTFrontendAction {
|
|
|
|
public:
|
|
|
|
ASTUnit &Unit;
|
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef InFile) {
|
2011-02-17 02:16:54 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(
|
|
|
|
new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
|
|
|
|
return new TopLevelDeclTrackerConsumer(Unit,
|
|
|
|
Unit.getCurrentTopLevelHashValue());
|
2009-12-01 17:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2009-12-04 16:17:33 +08:00
|
|
|
TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
|
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
virtual bool hasCodeCompletionSupport() const { return false; }
|
2011-08-26 06:30:56 +08:00
|
|
|
virtual TranslationUnitKind getTranslationUnitKind() {
|
|
|
|
return Unit.getTranslationUnitKind();
|
2010-08-10 04:45:32 +08:00
|
|
|
}
|
2009-12-01 17:51:01 +08:00
|
|
|
};
|
|
|
|
|
2013-06-11 21:07:19 +08:00
|
|
|
class PrecompilePreambleAction : public ASTFrontendAction {
|
|
|
|
ASTUnit &Unit;
|
|
|
|
bool HasEmittedPreamblePCH;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit PrecompilePreambleAction(ASTUnit &Unit)
|
|
|
|
: Unit(Unit), HasEmittedPreamblePCH(false) {}
|
|
|
|
|
|
|
|
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile);
|
|
|
|
bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
|
|
|
|
void setHasEmittedPreamblePCH() { HasEmittedPreamblePCH = true; }
|
|
|
|
virtual bool shouldEraseOutputFiles() { return !hasEmittedPreamblePCH(); }
|
|
|
|
|
|
|
|
virtual bool hasCodeCompletionSupport() const { return false; }
|
|
|
|
virtual bool hasASTFileSupport() const { return false; }
|
|
|
|
virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
|
|
|
|
};
|
|
|
|
|
2011-09-20 04:40:48 +08:00
|
|
|
class PrecompilePreambleConsumer : public PCHGenerator {
|
2010-08-03 16:14:03 +08:00
|
|
|
ASTUnit &Unit;
|
2013-06-11 21:07:19 +08:00
|
|
|
unsigned &Hash;
|
2010-08-04 03:06:41 +08:00
|
|
|
std::vector<Decl *> TopLevelDecls;
|
2013-06-11 21:07:19 +08:00
|
|
|
PrecompilePreambleAction *Action;
|
|
|
|
|
2010-08-03 16:14:03 +08:00
|
|
|
public:
|
2013-06-11 21:07:19 +08:00
|
|
|
PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action,
|
|
|
|
const Preprocessor &PP, StringRef isysroot,
|
|
|
|
raw_ostream *Out)
|
2013-06-11 08:36:55 +08:00
|
|
|
: PCHGenerator(PP, "", 0, isysroot, Out, /*AllowASTWithErrors=*/true),
|
2013-06-11 21:07:19 +08:00
|
|
|
Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action) {
|
2011-02-17 02:16:54 +08:00
|
|
|
Hash = 0;
|
|
|
|
}
|
2010-08-03 16:14:03 +08:00
|
|
|
|
2011-11-18 08:26:59 +08:00
|
|
|
virtual bool HandleTopLevelDecl(DeclGroupRef D) {
|
2010-08-03 16:14:03 +08:00
|
|
|
for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
|
|
|
|
Decl *D = *it;
|
|
|
|
// FIXME: Currently ObjC method declarations are incorrectly being
|
|
|
|
// reported as top-level declarations, even though their DeclContext
|
|
|
|
// is the containing ObjC @interface/@implementation. This is a
|
|
|
|
// fundamental problem in the parser right now.
|
|
|
|
if (isa<ObjCMethodDecl>(D))
|
|
|
|
continue;
|
2011-02-17 02:16:54 +08:00
|
|
|
AddTopLevelDeclarationToHash(D, Hash);
|
2010-08-04 03:06:41 +08:00
|
|
|
TopLevelDecls.push_back(D);
|
|
|
|
}
|
2011-11-18 08:26:59 +08:00
|
|
|
return true;
|
2010-08-04 03:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTranslationUnit(ASTContext &Ctx) {
|
|
|
|
PCHGenerator::HandleTranslationUnit(Ctx);
|
2013-06-11 08:36:55 +08:00
|
|
|
if (hasEmittedPCH()) {
|
2010-08-04 03:06:41 +08:00
|
|
|
// Translate the top-level declarations we captured during
|
|
|
|
// parsing into declaration IDs in the precompiled
|
|
|
|
// preamble. This will allow us to deserialize those top-level
|
|
|
|
// declarations when requested.
|
2013-08-08 05:17:33 +08:00
|
|
|
for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) {
|
|
|
|
Decl *D = TopLevelDecls[I];
|
|
|
|
// Invalid top-level decls may not have been serialized.
|
|
|
|
if (D->isInvalidDecl())
|
|
|
|
continue;
|
|
|
|
Unit.addTopLevelDeclFromPreamble(getWriter().getDeclID(D));
|
|
|
|
}
|
2013-06-11 21:07:19 +08:00
|
|
|
|
|
|
|
Action->setHasEmittedPreamblePCH();
|
2010-08-03 16:14:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-11 21:07:19 +08:00
|
|
|
}
|
2010-08-03 16:14:03 +08:00
|
|
|
|
2013-06-11 21:07:19 +08:00
|
|
|
ASTConsumer *PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
|
|
|
std::string Sysroot;
|
|
|
|
std::string OutputFile;
|
|
|
|
raw_ostream *OS = 0;
|
|
|
|
if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
|
|
|
|
OutputFile, OS))
|
|
|
|
return 0;
|
2013-06-11 08:36:55 +08:00
|
|
|
|
2013-06-11 21:07:19 +08:00
|
|
|
if (!CI.getFrontendOpts().RelocatablePCH)
|
|
|
|
Sysroot.clear();
|
2010-08-03 16:14:03 +08:00
|
|
|
|
2013-06-11 21:07:19 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(new MacroDefinitionTrackerPPCallbacks(
|
|
|
|
Unit.getCurrentTopLevelHashValue()));
|
|
|
|
return new PrecompilePreambleConsumer(Unit, this, CI.getPreprocessor(),
|
|
|
|
Sysroot, OS);
|
2009-12-01 17:51:01 +08:00
|
|
|
}
|
|
|
|
|
2013-05-05 20:39:28 +08:00
|
|
|
static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
|
|
|
|
return StoredDiag.getLocation().isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
|
2012-02-02 03:54:02 +08:00
|
|
|
// Get rid of stored diagnostics except the ones from the driver which do not
|
|
|
|
// have a source location.
|
2013-05-05 20:39:28 +08:00
|
|
|
StoredDiags.erase(
|
|
|
|
std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
|
|
|
|
StoredDiags.end());
|
2012-02-02 03:54:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
|
|
|
|
StoredDiagnostics,
|
|
|
|
SourceManager &SM) {
|
|
|
|
// The stored diagnostic has the old source manager in it; update
|
|
|
|
// the locations to refer into the new source manager. Since we've
|
|
|
|
// been careful to make sure that the source manager's state
|
|
|
|
// before and after are identical, so that we can reuse the source
|
|
|
|
// location itself.
|
|
|
|
for (unsigned I = 0, N = StoredDiagnostics.size(); I < N; ++I) {
|
|
|
|
if (StoredDiagnostics[I].getLocation().isValid()) {
|
|
|
|
FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SM);
|
|
|
|
StoredDiagnostics[I].setLocation(Loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
/// Parse the source file into a translation unit using the given compiler
|
|
|
|
/// invocation, replacing the current translation unit.
|
|
|
|
///
|
|
|
|
/// \returns True if a failure occurred that causes the ASTUnit not to
|
|
|
|
/// contain any translation-unit information, false otherwise.
|
2010-07-24 08:38:13 +08:00
|
|
|
bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
|
2010-07-27 22:52:07 +08:00
|
|
|
delete SavedMainFileBuffer;
|
|
|
|
SavedMainFileBuffer = 0;
|
|
|
|
|
2011-03-22 02:40:17 +08:00
|
|
|
if (!Invocation) {
|
2010-08-19 09:33:06 +08:00
|
|
|
delete OverrideMainBuffer;
|
2010-07-20 05:46:24 +08:00
|
|
|
return true;
|
2010-08-19 09:33:06 +08:00
|
|
|
}
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
// Create the compiler instance to use for building the AST.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<CompilerInstance> Clang(new CompilerInstance());
|
2011-03-22 02:40:07 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2011-03-22 09:15:24 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
|
|
|
|
CICleanup(Clang.get());
|
2011-03-22 02:40:07 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<CompilerInvocation>
|
2011-09-13 02:09:38 +08:00
|
|
|
CCInvocation(new CompilerInvocation(*Invocation));
|
|
|
|
|
|
|
|
Clang->setInvocation(CCInvocation.getPtr());
|
2012-11-10 03:40:39 +08:00
|
|
|
OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2010-08-05 00:47:14 +08:00
|
|
|
// Set up diagnostics, capturing any diagnostics that would
|
|
|
|
// otherwise be dropped.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setDiagnostics(&getDiagnostics());
|
2010-04-06 05:10:19 +08:00
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
// Create the target instance.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
|
2012-11-16 12:24:59 +08:00
|
|
|
&Clang->getTargetOpts()));
|
2011-03-22 02:40:07 +08:00
|
|
|
if (!Clang->hasTarget()) {
|
2010-08-19 09:33:06 +08:00
|
|
|
delete OverrideMainBuffer;
|
2010-07-20 05:46:24 +08:00
|
|
|
return true;
|
2010-08-19 09:33:06 +08:00
|
|
|
}
|
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
// Inform the target of the language options.
|
|
|
|
//
|
|
|
|
// FIXME: We shouldn't need to do this, the target should be immutable once
|
|
|
|
// created. This complexity should be lifted elsewhere.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2011-03-22 02:40:07 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
|
2009-12-01 17:51:01 +08:00
|
|
|
"Invocation must have exactly one source file!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
|
2009-12-01 17:51:01 +08:00
|
|
|
"FIXME: AST inputs not yet supported here!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
|
2010-06-08 07:26:47 +08:00
|
|
|
"IR inputs not support here!");
|
2009-12-01 17:51:01 +08:00
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
// Configure the various subsystems.
|
|
|
|
// FIXME: Should we retain the previous file manager?
|
2011-11-18 07:01:24 +08:00
|
|
|
LangOpts = &Clang->getLangOpts();
|
2011-03-22 02:40:07 +08:00
|
|
|
FileSystemOpts = Clang->getFileSystemOpts();
|
2011-03-22 02:40:17 +08:00
|
|
|
FileMgr = new FileManager(FileSystemOpts);
|
2012-07-12 04:59:04 +08:00
|
|
|
SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
|
|
|
|
UserFilesAreVolatile);
|
2010-08-13 11:15:25 +08:00
|
|
|
TheSema.reset();
|
2011-03-22 02:40:17 +08:00
|
|
|
Ctx = 0;
|
|
|
|
PP = 0;
|
2011-11-02 01:14:15 +08:00
|
|
|
Reader = 0;
|
2010-07-20 05:46:24 +08:00
|
|
|
|
|
|
|
// Clear out old caches and data.
|
|
|
|
TopLevelDecls.clear();
|
2011-10-31 15:19:59 +08:00
|
|
|
clearFileLevelDecls();
|
2010-07-20 05:46:24 +08:00
|
|
|
CleanTemporaryFiles();
|
2010-08-03 04:51:39 +08:00
|
|
|
|
2010-08-20 08:02:33 +08:00
|
|
|
if (!OverrideMainBuffer) {
|
2012-02-02 03:54:02 +08:00
|
|
|
checkAndRemoveNonDriverDiags(StoredDiagnostics);
|
2010-08-20 08:02:33 +08:00
|
|
|
TopLevelDeclsInPreamble.clear();
|
|
|
|
}
|
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
// Create a file manager object to provide access to and cache the filesystem.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setFileManager(&getFileManager());
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
// Create the source manager.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setSourceManager(&getSourceManager());
|
2010-07-20 05:46:24 +08:00
|
|
|
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
// If the main file has been overridden due to the use of a preamble,
|
|
|
|
// make that override happen and introduce the preamble.
|
2011-03-22 02:40:07 +08:00
|
|
|
PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
if (OverrideMainBuffer) {
|
|
|
|
PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
|
|
|
|
PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
|
|
|
|
PreprocessorOpts.PrecompiledPreambleBytes.second
|
|
|
|
= PreambleEndsAtStartOfLine;
|
2011-10-28 01:55:18 +08:00
|
|
|
PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
|
2010-07-27 08:27:13 +08:00
|
|
|
PreprocessorOpts.DisablePCHValidation = true;
|
2010-07-27 22:52:07 +08:00
|
|
|
|
2010-08-03 04:51:39 +08:00
|
|
|
// The stored diagnostic has the old source manager in it; update
|
|
|
|
// the locations to refer into the new source manager. Since we've
|
|
|
|
// been careful to make sure that the source manager's state
|
|
|
|
// before and after are identical, so that we can reuse the source
|
|
|
|
// location itself.
|
2012-02-02 03:54:02 +08:00
|
|
|
checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
|
2010-10-12 08:50:20 +08:00
|
|
|
|
|
|
|
// Keep track of the override buffer;
|
|
|
|
SavedMainFileBuffer = OverrideMainBuffer;
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
}
|
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<TopLevelDeclTrackerAction> Act(
|
2011-03-22 09:15:24 +08:00
|
|
|
new TopLevelDeclTrackerAction(*this));
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
|
|
|
|
ActCleanup(Act.get());
|
|
|
|
|
2012-01-21 00:28:04 +08:00
|
|
|
if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
|
2009-12-01 17:51:01 +08:00
|
|
|
goto error;
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
|
|
|
|
if (OverrideMainBuffer) {
|
2011-10-28 01:55:18 +08:00
|
|
|
std::string ModName = getPreambleFile(this);
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
|
|
|
|
getSourceManager(), PreambleDiagnostics,
|
|
|
|
StoredDiagnostics);
|
|
|
|
}
|
|
|
|
|
2012-06-08 13:48:06 +08:00
|
|
|
if (!Act->Execute())
|
|
|
|
goto error;
|
2012-04-11 10:11:16 +08:00
|
|
|
|
|
|
|
transferASTDataFromCompilerInstance(*Clang);
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2009-12-04 16:17:33 +08:00
|
|
|
Act->EndSourceFile();
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
|
2012-04-11 10:11:16 +08:00
|
|
|
FailedParseDiagnostics.clear();
|
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
return false;
|
2011-03-22 02:40:17 +08:00
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
error:
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
// Remove the overridden buffer we used for the preamble.
|
2010-07-27 08:27:13 +08:00
|
|
|
if (OverrideMainBuffer) {
|
2010-08-19 09:33:06 +08:00
|
|
|
delete OverrideMainBuffer;
|
2010-10-07 05:11:08 +08:00
|
|
|
SavedMainFileBuffer = 0;
|
2010-07-27 08:27:13 +08:00
|
|
|
}
|
2012-04-11 10:11:16 +08:00
|
|
|
|
|
|
|
// Keep the ownership of the data in the ASTUnit because the client may
|
|
|
|
// want to see the diagnostics.
|
|
|
|
transferASTDataFromCompilerInstance(*Clang);
|
|
|
|
FailedParseDiagnostics.swap(StoredDiagnostics);
|
2010-10-13 00:25:54 +08:00
|
|
|
StoredDiagnostics.clear();
|
2011-10-25 01:25:20 +08:00
|
|
|
NumStoredDiagnosticsFromDriver = 0;
|
2010-07-20 05:46:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-23 08:33:23 +08:00
|
|
|
/// \brief Simple function to retrieve a path for a preamble precompiled header.
|
|
|
|
static std::string GetPreamblePCHPath() {
|
2010-09-12 02:05:19 +08:00
|
|
|
// FIXME: This is a hack so that we can override the preamble file during
|
|
|
|
// crash-recovery testing, which is the only case where the preamble files
|
2013-06-26 12:02:37 +08:00
|
|
|
// are not necessarily cleaned up.
|
2010-09-12 02:05:19 +08:00
|
|
|
const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
|
|
|
|
if (TmpFile)
|
|
|
|
return TmpFile;
|
2013-06-26 12:02:37 +08:00
|
|
|
|
|
|
|
SmallString<128> Path;
|
2013-07-06 04:00:06 +08:00
|
|
|
llvm::sys::fs::createTemporaryFile("preamble", "pch", Path);
|
2013-06-26 12:02:37 +08:00
|
|
|
|
|
|
|
return Path.str();
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
|
|
|
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
/// \brief Compute the preamble for the main file, providing the source buffer
|
|
|
|
/// that corresponds to the main file along with a pair (bytes, start-of-line)
|
|
|
|
/// that describes the preamble.
|
|
|
|
std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
|
2010-08-10 04:45:32 +08:00
|
|
|
ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
|
|
|
|
unsigned MaxLines, bool &CreatedBuffer) {
|
2010-07-24 07:58:40 +08:00
|
|
|
FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
|
2010-11-23 16:35:12 +08:00
|
|
|
PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
|
2010-07-24 07:58:40 +08:00
|
|
|
CreatedBuffer = false;
|
|
|
|
|
2010-07-23 08:33:23 +08:00
|
|
|
// Try to determine if the main file has been remapped, either from the
|
|
|
|
// command line (to another file) or directly through the compiler invocation
|
|
|
|
// (to a memory buffer).
|
2010-07-24 07:58:40 +08:00
|
|
|
llvm::MemoryBuffer *Buffer = 0;
|
2013-06-19 03:40:07 +08:00
|
|
|
std::string MainFilePath(FrontendOpts.Inputs[0].getFile());
|
2013-07-30 05:26:52 +08:00
|
|
|
llvm::sys::fs::UniqueID MainFileID;
|
2013-06-20 23:12:38 +08:00
|
|
|
if (!llvm::sys::fs::getUniqueID(MainFilePath, MainFileID)) {
|
2010-07-23 08:33:23 +08:00
|
|
|
// Check whether there is a file-file remapping of the main file
|
|
|
|
for (PreprocessorOptions::remapped_file_iterator
|
2010-07-24 07:58:40 +08:00
|
|
|
M = PreprocessorOpts.remapped_file_begin(),
|
|
|
|
E = PreprocessorOpts.remapped_file_end();
|
2010-07-23 08:33:23 +08:00
|
|
|
M != E;
|
|
|
|
++M) {
|
2013-06-19 03:40:07 +08:00
|
|
|
std::string MPath(M->first);
|
2013-07-30 05:26:52 +08:00
|
|
|
llvm::sys::fs::UniqueID MID;
|
2013-06-20 23:12:38 +08:00
|
|
|
if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
|
2013-06-19 03:40:07 +08:00
|
|
|
if (MainFileID == MID) {
|
2010-07-23 08:33:23 +08:00
|
|
|
// We found a remapping. Try to load the resulting, remapped source.
|
2010-07-24 07:58:40 +08:00
|
|
|
if (CreatedBuffer) {
|
2010-07-23 08:33:23 +08:00
|
|
|
delete Buffer;
|
2010-07-24 07:58:40 +08:00
|
|
|
CreatedBuffer = false;
|
|
|
|
}
|
|
|
|
|
2010-11-04 06:45:23 +08:00
|
|
|
Buffer = getBufferForFile(M->second);
|
2010-07-23 08:33:23 +08:00
|
|
|
if (!Buffer)
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
return std::make_pair((llvm::MemoryBuffer*)0,
|
|
|
|
std::make_pair(0, true));
|
2010-07-24 07:58:40 +08:00
|
|
|
CreatedBuffer = true;
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether there is a file-buffer remapping. It supercedes the
|
|
|
|
// file-file remapping.
|
|
|
|
for (PreprocessorOptions::remapped_file_buffer_iterator
|
|
|
|
M = PreprocessorOpts.remapped_file_buffer_begin(),
|
|
|
|
E = PreprocessorOpts.remapped_file_buffer_end();
|
|
|
|
M != E;
|
|
|
|
++M) {
|
2013-06-19 03:40:07 +08:00
|
|
|
std::string MPath(M->first);
|
2013-07-30 05:26:52 +08:00
|
|
|
llvm::sys::fs::UniqueID MID;
|
2013-06-20 23:12:38 +08:00
|
|
|
if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
|
2013-06-19 03:40:07 +08:00
|
|
|
if (MainFileID == MID) {
|
|
|
|
// We found a remapping.
|
2010-07-24 07:58:40 +08:00
|
|
|
if (CreatedBuffer) {
|
2010-07-23 08:33:23 +08:00
|
|
|
delete Buffer;
|
2010-07-24 07:58:40 +08:00
|
|
|
CreatedBuffer = false;
|
|
|
|
}
|
2010-07-23 08:33:23 +08:00
|
|
|
|
2010-07-24 07:58:40 +08:00
|
|
|
Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-24 07:58:40 +08:00
|
|
|
}
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the main source file was not remapped, load it now.
|
|
|
|
if (!Buffer) {
|
2012-11-10 03:40:39 +08:00
|
|
|
Buffer = getBufferForFile(FrontendOpts.Inputs[0].getFile());
|
2010-07-23 08:33:23 +08:00
|
|
|
if (!Buffer)
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
|
2010-07-24 07:58:40 +08:00
|
|
|
|
|
|
|
CreatedBuffer = true;
|
|
|
|
}
|
|
|
|
|
2011-08-26 04:39:19 +08:00
|
|
|
return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer,
|
2011-11-18 07:01:24 +08:00
|
|
|
*Invocation.getLangOpts(),
|
2011-08-26 04:39:19 +08:00
|
|
|
MaxLines));
|
2010-07-24 07:58:40 +08:00
|
|
|
}
|
|
|
|
|
2010-07-24 08:38:13 +08:00
|
|
|
static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
|
|
|
|
unsigned NewSize,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef NewName) {
|
2010-07-24 08:38:13 +08:00
|
|
|
llvm::MemoryBuffer *Result
|
|
|
|
= llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
|
|
|
|
memcpy(const_cast<char*>(Result->getBufferStart()),
|
|
|
|
Old->getBufferStart(), Old->getBufferSize());
|
|
|
|
memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
' ', NewSize - Old->getBufferSize() - 1);
|
|
|
|
const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
|
2010-07-24 08:38:13 +08:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2013-12-20 08:16:25 +08:00
|
|
|
ASTUnit::PreambleFileHash
|
|
|
|
ASTUnit::PreambleFileHash::createForFile(off_t Size, time_t ModTime) {
|
|
|
|
PreambleFileHash Result;
|
|
|
|
Result.Size = Size;
|
|
|
|
Result.ModTime = ModTime;
|
2013-12-20 09:07:30 +08:00
|
|
|
memset(Result.MD5, 0, sizeof(Result.MD5));
|
2013-12-20 08:16:25 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTUnit::PreambleFileHash ASTUnit::PreambleFileHash::createForMemoryBuffer(
|
|
|
|
const llvm::MemoryBuffer *Buffer) {
|
|
|
|
PreambleFileHash Result;
|
|
|
|
Result.Size = Buffer->getBufferSize();
|
|
|
|
Result.ModTime = 0;
|
|
|
|
|
|
|
|
llvm::MD5 MD5Ctx;
|
|
|
|
MD5Ctx.update(Buffer->getBuffer().data());
|
|
|
|
MD5Ctx.final(Result.MD5);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
bool operator==(const ASTUnit::PreambleFileHash &LHS,
|
|
|
|
const ASTUnit::PreambleFileHash &RHS) {
|
|
|
|
return LHS.Size == RHS.Size && LHS.ModTime == RHS.ModTime &&
|
2013-12-20 09:07:30 +08:00
|
|
|
memcmp(LHS.MD5, RHS.MD5, sizeof(LHS.MD5)) == 0;
|
2013-12-20 08:16:25 +08:00
|
|
|
}
|
|
|
|
} // namespace clang
|
|
|
|
|
2010-07-24 07:58:40 +08:00
|
|
|
/// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
|
|
|
|
/// the source file.
|
|
|
|
///
|
|
|
|
/// This routine will compute the preamble of the main source file. If a
|
|
|
|
/// non-trivial preamble is found, it will precompile that preamble into a
|
|
|
|
/// precompiled header so that the precompiled preamble can be used to reduce
|
|
|
|
/// reparsing time. If a precompiled preamble has already been constructed,
|
|
|
|
/// this routine will determine if it is still valid and, if so, avoid
|
|
|
|
/// rebuilding the precompiled preamble.
|
|
|
|
///
|
2010-08-10 04:45:32 +08:00
|
|
|
/// \param AllowRebuild When true (the default), this routine is
|
|
|
|
/// allowed to rebuild the precompiled preamble if it is found to be
|
|
|
|
/// out-of-date.
|
|
|
|
///
|
|
|
|
/// \param MaxLines When non-zero, the maximum number of lines that
|
|
|
|
/// can occur within the preamble.
|
|
|
|
///
|
2010-07-24 08:38:13 +08:00
|
|
|
/// \returns If the precompiled preamble can be used, returns a newly-allocated
|
|
|
|
/// buffer that should be used in place of the main file when doing so.
|
|
|
|
/// Otherwise, returns a NULL pointer.
|
2010-08-10 04:45:32 +08:00
|
|
|
llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
|
2011-07-02 02:22:13 +08:00
|
|
|
const CompilerInvocation &PreambleInvocationIn,
|
2010-08-10 04:45:32 +08:00
|
|
|
bool AllowRebuild,
|
|
|
|
unsigned MaxLines) {
|
2011-07-02 02:22:13 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<CompilerInvocation>
|
2011-07-02 02:22:13 +08:00
|
|
|
PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
|
|
|
|
FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
|
2010-07-24 07:58:40 +08:00
|
|
|
PreprocessorOptions &PreprocessorOpts
|
2011-07-02 02:22:13 +08:00
|
|
|
= PreambleInvocation->getPreprocessorOpts();
|
2010-07-24 07:58:40 +08:00
|
|
|
|
|
|
|
bool CreatedPreambleBuffer = false;
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
|
2011-07-02 02:22:13 +08:00
|
|
|
= ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer);
|
2010-07-24 07:58:40 +08:00
|
|
|
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
// If ComputePreamble() Take ownership of the preamble buffer.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
|
2010-11-17 04:45:51 +08:00
|
|
|
if (CreatedPreambleBuffer)
|
|
|
|
OwnedPreambleBuffer.reset(NewPreamble.first);
|
|
|
|
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
if (!NewPreamble.second.first) {
|
2010-07-24 07:58:40 +08:00
|
|
|
// We couldn't find a preamble in the main source. Clear out the current
|
|
|
|
// preamble, if we have one. It's obviously no good any more.
|
|
|
|
Preamble.clear();
|
2011-10-28 01:55:18 +08:00
|
|
|
erasePreambleFile(this);
|
2010-08-04 13:53:38 +08:00
|
|
|
|
|
|
|
// The next time we actually see a preamble, precompile it.
|
|
|
|
PreambleRebuildCounter = 1;
|
2010-07-24 08:38:13 +08:00
|
|
|
return 0;
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
|
|
|
|
2010-07-24 07:58:40 +08:00
|
|
|
if (!Preamble.empty()) {
|
|
|
|
// We've previously computed a preamble. Check whether we have the same
|
|
|
|
// preamble now that we did before, and that there's enough space in
|
|
|
|
// the main-file buffer within the precompiled preamble to fit the
|
|
|
|
// new main file.
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
if (Preamble.size() == NewPreamble.second.first &&
|
|
|
|
PreambleEndsAtStartOfLine == NewPreamble.second.second &&
|
2010-07-24 08:42:07 +08:00
|
|
|
NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
|
2011-09-20 04:40:35 +08:00
|
|
|
memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
NewPreamble.second.first) == 0) {
|
2010-07-24 07:58:40 +08:00
|
|
|
// The preamble has not changed. We may be able to re-use the precompiled
|
|
|
|
// preamble.
|
2010-08-03 04:51:39 +08:00
|
|
|
|
2010-07-31 08:40:00 +08:00
|
|
|
// Check that none of the files used by the preamble have changed.
|
|
|
|
bool AnyFileChanged = false;
|
|
|
|
|
|
|
|
// First, make a record of those files that have been overridden via
|
|
|
|
// remapping or unsaved_files.
|
2013-12-20 08:16:25 +08:00
|
|
|
llvm::StringMap<PreambleFileHash> OverriddenFiles;
|
2010-07-31 08:40:00 +08:00
|
|
|
for (PreprocessorOptions::remapped_file_iterator
|
|
|
|
R = PreprocessorOpts.remapped_file_begin(),
|
|
|
|
REnd = PreprocessorOpts.remapped_file_end();
|
|
|
|
!AnyFileChanged && R != REnd;
|
|
|
|
++R) {
|
2013-07-30 02:22:23 +08:00
|
|
|
llvm::sys::fs::file_status Status;
|
|
|
|
if (FileMgr->getNoncachedStatValue(R->second, Status)) {
|
2010-07-31 08:40:00 +08:00
|
|
|
// If we can't stat the file we're remapping to, assume that something
|
|
|
|
// horrible happened.
|
|
|
|
AnyFileChanged = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-30 02:22:23 +08:00
|
|
|
|
2013-12-20 08:16:25 +08:00
|
|
|
OverriddenFiles[R->first] = PreambleFileHash::createForFile(
|
2013-07-30 02:22:23 +08:00
|
|
|
Status.getSize(), Status.getLastModificationTime().toEpochTime());
|
2010-07-31 08:40:00 +08:00
|
|
|
}
|
|
|
|
for (PreprocessorOptions::remapped_file_buffer_iterator
|
|
|
|
R = PreprocessorOpts.remapped_file_buffer_begin(),
|
|
|
|
REnd = PreprocessorOpts.remapped_file_buffer_end();
|
|
|
|
!AnyFileChanged && R != REnd;
|
|
|
|
++R) {
|
2013-12-20 08:16:25 +08:00
|
|
|
OverriddenFiles[R->first] =
|
|
|
|
PreambleFileHash::createForMemoryBuffer(R->second);
|
2010-07-31 08:40:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether anything has changed.
|
2013-12-20 08:16:25 +08:00
|
|
|
for (llvm::StringMap<PreambleFileHash>::iterator
|
2010-07-31 08:40:00 +08:00
|
|
|
F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
|
|
|
|
!AnyFileChanged && F != FEnd;
|
|
|
|
++F) {
|
2013-12-20 08:16:25 +08:00
|
|
|
llvm::StringMap<PreambleFileHash>::iterator Overridden
|
2010-07-31 08:40:00 +08:00
|
|
|
= OverriddenFiles.find(F->first());
|
|
|
|
if (Overridden != OverriddenFiles.end()) {
|
|
|
|
// This file was remapped; check whether the newly-mapped file
|
|
|
|
// matches up with the previous mapping.
|
|
|
|
if (Overridden->second != F->second)
|
|
|
|
AnyFileChanged = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file was not remapped; check whether it has changed on disk.
|
2013-07-30 02:22:23 +08:00
|
|
|
llvm::sys::fs::file_status Status;
|
|
|
|
if (FileMgr->getNoncachedStatValue(F->first(), Status)) {
|
2010-07-31 08:40:00 +08:00
|
|
|
// If we can't stat the file, assume that something horrible happened.
|
|
|
|
AnyFileChanged = true;
|
2013-12-20 08:16:25 +08:00
|
|
|
} else if (Status.getSize() != uint64_t(F->second.Size) ||
|
2013-07-30 02:22:23 +08:00
|
|
|
Status.getLastModificationTime().toEpochTime() !=
|
2013-12-20 08:16:25 +08:00
|
|
|
uint64_t(F->second.ModTime))
|
2010-07-31 08:40:00 +08:00
|
|
|
AnyFileChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AnyFileChanged) {
|
2010-08-03 04:51:39 +08:00
|
|
|
// Okay! We can re-use the precompiled preamble.
|
|
|
|
|
|
|
|
// Set the state of the diagnostic object to mimic its state
|
|
|
|
// after parsing the preamble.
|
|
|
|
getDiagnostics().Reset();
|
2010-10-12 05:37:58 +08:00
|
|
|
ProcessWarningOptions(getDiagnostics(),
|
2011-07-02 02:22:13 +08:00
|
|
|
PreambleInvocation->getDiagnosticOpts());
|
2010-08-03 04:51:39 +08:00
|
|
|
getDiagnostics().setNumWarnings(NumWarningsInPreamble);
|
|
|
|
|
|
|
|
// Create a version of the main file buffer that is padded to
|
|
|
|
// buffer size we reserved when creating the preamble.
|
2010-07-31 08:40:00 +08:00
|
|
|
return CreatePaddedMainFileBuffer(NewPreamble.first,
|
|
|
|
PreambleReservedSize,
|
2012-11-10 03:40:39 +08:00
|
|
|
FrontendOpts.Inputs[0].getFile());
|
2010-07-31 08:40:00 +08:00
|
|
|
}
|
2010-07-24 07:58:40 +08:00
|
|
|
}
|
2010-08-10 04:45:32 +08:00
|
|
|
|
|
|
|
// If we aren't allowed to rebuild the precompiled preamble, just
|
|
|
|
// return now.
|
|
|
|
if (!AllowRebuild)
|
|
|
|
return 0;
|
2010-10-08 12:03:57 +08:00
|
|
|
|
2010-07-24 07:58:40 +08:00
|
|
|
// We can't reuse the previously-computed preamble. Build a new one.
|
|
|
|
Preamble.clear();
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
PreambleDiagnostics.clear();
|
2011-10-28 01:55:18 +08:00
|
|
|
erasePreambleFile(this);
|
2010-08-04 13:53:38 +08:00
|
|
|
PreambleRebuildCounter = 1;
|
2010-08-10 04:45:32 +08:00
|
|
|
} else if (!AllowRebuild) {
|
|
|
|
// We aren't allowed to rebuild the precompiled preamble; just
|
|
|
|
// return now.
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-04 13:53:38 +08:00
|
|
|
|
|
|
|
// If the preamble rebuild counter > 1, it's because we previously
|
|
|
|
// failed to build a preamble and we're not yet ready to try
|
|
|
|
// again. Decrement the counter and return a failure.
|
|
|
|
if (PreambleRebuildCounter > 1) {
|
|
|
|
--PreambleRebuildCounter;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-12 01:56:52 +08:00
|
|
|
// Create a temporary file for the precompiled preamble. In rare
|
|
|
|
// circumstances, this can fail.
|
|
|
|
std::string PreamblePCHPath = GetPreamblePCHPath();
|
|
|
|
if (PreamblePCHPath.empty()) {
|
|
|
|
// Try again next time.
|
|
|
|
PreambleRebuildCounter = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-24 07:58:40 +08:00
|
|
|
// We did not previously compute a preamble, or it can't be reused anyway.
|
2010-10-28 23:44:59 +08:00
|
|
|
SimpleTimer PreambleTimer(WantTiming);
|
2010-11-10 04:00:56 +08:00
|
|
|
PreambleTimer.setOutput("Precompiling preamble");
|
2010-07-23 08:33:23 +08:00
|
|
|
|
|
|
|
// Create a new buffer that stores the preamble. The buffer also contains
|
|
|
|
// extra space for the original contents of the file (which will be present
|
|
|
|
// when we actually parse the file) along with more room in case the file
|
2010-07-24 07:58:40 +08:00
|
|
|
// grows.
|
|
|
|
PreambleReservedSize = NewPreamble.first->getBufferSize();
|
|
|
|
if (PreambleReservedSize < 4096)
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
PreambleReservedSize = 8191;
|
2010-07-23 08:33:23 +08:00
|
|
|
else
|
2010-07-24 07:58:40 +08:00
|
|
|
PreambleReservedSize *= 2;
|
|
|
|
|
2010-08-03 04:51:39 +08:00
|
|
|
// Save the preamble text for later; we'll need to compare against it for
|
|
|
|
// subsequent reparses.
|
2013-12-20 07:25:59 +08:00
|
|
|
StringRef MainFilename = FrontendOpts.Inputs[0].getFile();
|
2011-09-20 04:40:35 +08:00
|
|
|
Preamble.assign(FileMgr->getFile(MainFilename),
|
|
|
|
NewPreamble.first->getBufferStart(),
|
2010-08-03 04:51:39 +08:00
|
|
|
NewPreamble.first->getBufferStart()
|
|
|
|
+ NewPreamble.second.first);
|
|
|
|
PreambleEndsAtStartOfLine = NewPreamble.second.second;
|
|
|
|
|
2010-08-19 09:33:06 +08:00
|
|
|
delete PreambleBuffer;
|
|
|
|
PreambleBuffer
|
2010-07-24 07:58:40 +08:00
|
|
|
= llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
|
2012-11-10 03:40:39 +08:00
|
|
|
FrontendOpts.Inputs[0].getFile());
|
2010-07-23 08:33:23 +08:00
|
|
|
memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
|
2010-07-24 07:58:40 +08:00
|
|
|
NewPreamble.first->getBufferStart(), Preamble.size());
|
|
|
|
memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
' ', PreambleReservedSize - Preamble.size() - 1);
|
|
|
|
const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
|
2013-06-26 12:12:57 +08:00
|
|
|
|
2010-07-23 08:33:23 +08:00
|
|
|
// Remap the main source file to the preamble buffer.
|
2013-06-26 12:12:57 +08:00
|
|
|
StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
|
|
|
|
PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer);
|
|
|
|
|
2010-07-23 08:33:23 +08:00
|
|
|
// Tell the compiler invocation to generate a temporary precompiled header.
|
|
|
|
FrontendOpts.ProgramAction = frontend::GeneratePCH;
|
|
|
|
// FIXME: Generate the precompiled header into memory?
|
2010-09-12 01:56:52 +08:00
|
|
|
FrontendOpts.OutputFile = PreamblePCHPath;
|
2010-10-08 12:03:57 +08:00
|
|
|
PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
|
|
|
|
PreprocessorOpts.PrecompiledPreambleBytes.second = false;
|
2010-07-23 08:33:23 +08:00
|
|
|
|
|
|
|
// Create the compiler instance to use for building the precompiled preamble.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<CompilerInstance> Clang(new CompilerInstance());
|
2011-03-22 02:40:07 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2011-03-22 09:15:24 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
|
|
|
|
CICleanup(Clang.get());
|
2011-03-22 02:40:07 +08:00
|
|
|
|
2011-07-02 02:22:13 +08:00
|
|
|
Clang->setInvocation(&*PreambleInvocation);
|
2012-11-10 03:40:39 +08:00
|
|
|
OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
|
2010-07-23 08:33:23 +08:00
|
|
|
|
2010-08-05 00:47:14 +08:00
|
|
|
// Set up diagnostics, capturing all of the diagnostics produced.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setDiagnostics(&getDiagnostics());
|
2010-07-23 08:33:23 +08:00
|
|
|
|
|
|
|
// Create the target instance.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
|
2012-11-16 12:24:59 +08:00
|
|
|
&Clang->getTargetOpts()));
|
2011-03-22 02:40:07 +08:00
|
|
|
if (!Clang->hasTarget()) {
|
2013-06-26 12:26:38 +08:00
|
|
|
llvm::sys::fs::remove(FrontendOpts.OutputFile);
|
2010-07-24 07:58:40 +08:00
|
|
|
Preamble.clear();
|
2010-08-04 13:53:38 +08:00
|
|
|
PreambleRebuildCounter = DefaultPreambleRebuildInterval;
|
2010-08-19 09:33:06 +08:00
|
|
|
PreprocessorOpts.eraseRemappedFile(
|
|
|
|
PreprocessorOpts.remapped_file_buffer_end() - 1);
|
2010-07-24 08:38:13 +08:00
|
|
|
return 0;
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inform the target of the language options.
|
|
|
|
//
|
|
|
|
// FIXME: We shouldn't need to do this, the target should be immutable once
|
|
|
|
// created. This complexity should be lifted elsewhere.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
|
2010-07-23 08:33:23 +08:00
|
|
|
|
2011-03-22 02:40:07 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
|
2010-07-23 08:33:23 +08:00
|
|
|
"Invocation must have exactly one source file!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
|
2010-07-23 08:33:23 +08:00
|
|
|
"FIXME: AST inputs not yet supported here!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
|
2010-07-23 08:33:23 +08:00
|
|
|
"IR inputs not support here!");
|
|
|
|
|
|
|
|
// Clear out old caches and data.
|
2010-10-08 12:03:57 +08:00
|
|
|
getDiagnostics().Reset();
|
2011-03-22 02:40:07 +08:00
|
|
|
ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
|
2012-02-02 03:54:02 +08:00
|
|
|
checkAndRemoveNonDriverDiags(StoredDiagnostics);
|
2010-08-04 03:06:41 +08:00
|
|
|
TopLevelDecls.clear();
|
|
|
|
TopLevelDeclsInPreamble.clear();
|
2010-07-23 08:33:23 +08:00
|
|
|
|
|
|
|
// Create a file manager object to provide access to and cache the filesystem.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
|
2010-07-23 08:33:23 +08:00
|
|
|
|
|
|
|
// Create the source manager.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setSourceManager(new SourceManager(getDiagnostics(),
|
2011-03-22 02:40:17 +08:00
|
|
|
Clang->getFileManager()));
|
2010-07-23 08:33:23 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<PrecompilePreambleAction> Act;
|
2010-08-03 16:14:03 +08:00
|
|
|
Act.reset(new PrecompilePreambleAction(*this));
|
2012-01-21 00:28:04 +08:00
|
|
|
if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
|
2013-06-26 12:26:38 +08:00
|
|
|
llvm::sys::fs::remove(FrontendOpts.OutputFile);
|
2010-07-24 07:58:40 +08:00
|
|
|
Preamble.clear();
|
2010-08-04 13:53:38 +08:00
|
|
|
PreambleRebuildCounter = DefaultPreambleRebuildInterval;
|
2010-08-19 09:33:06 +08:00
|
|
|
PreprocessorOpts.eraseRemappedFile(
|
|
|
|
PreprocessorOpts.remapped_file_buffer_end() - 1);
|
2010-07-24 08:38:13 +08:00
|
|
|
return 0;
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Act->Execute();
|
|
|
|
Act->EndSourceFile();
|
2011-03-22 02:40:17 +08:00
|
|
|
|
2013-06-11 08:36:55 +08:00
|
|
|
if (!Act->hasEmittedPreamblePCH()) {
|
2013-06-12 00:42:34 +08:00
|
|
|
// The preamble PCH failed (e.g. there was a module loading fatal error),
|
|
|
|
// so no precompiled header was generated. Forget that we even tried.
|
2010-09-28 00:43:25 +08:00
|
|
|
// FIXME: Should we leave a note for ourselves to try again?
|
2013-06-26 12:26:38 +08:00
|
|
|
llvm::sys::fs::remove(FrontendOpts.OutputFile);
|
2010-07-24 07:58:40 +08:00
|
|
|
Preamble.clear();
|
2010-08-04 03:06:41 +08:00
|
|
|
TopLevelDeclsInPreamble.clear();
|
2010-08-04 13:53:38 +08:00
|
|
|
PreambleRebuildCounter = DefaultPreambleRebuildInterval;
|
2010-08-19 09:33:06 +08:00
|
|
|
PreprocessorOpts.eraseRemappedFile(
|
|
|
|
PreprocessorOpts.remapped_file_buffer_end() - 1);
|
2010-07-24 08:38:13 +08:00
|
|
|
return 0;
|
2010-07-24 07:58:40 +08:00
|
|
|
}
|
|
|
|
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
// Transfer any diagnostics generated when parsing the preamble into the set
|
|
|
|
// of preamble diagnostics.
|
|
|
|
PreambleDiagnostics.clear();
|
|
|
|
PreambleDiagnostics.insert(PreambleDiagnostics.end(),
|
2011-10-25 01:25:20 +08:00
|
|
|
stored_diag_afterDriver_begin(), stored_diag_end());
|
2012-02-02 03:54:02 +08:00
|
|
|
checkAndRemoveNonDriverDiags(StoredDiagnostics);
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
|
2010-07-24 07:58:40 +08:00
|
|
|
// Keep track of the preamble we precompiled.
|
2011-10-28 01:55:18 +08:00
|
|
|
setPreambleFile(this, FrontendOpts.OutputFile);
|
2010-08-03 04:51:39 +08:00
|
|
|
NumWarningsInPreamble = getDiagnostics().getNumWarnings();
|
2010-07-31 08:40:00 +08:00
|
|
|
|
|
|
|
// Keep track of all of the files that the source manager knows about,
|
|
|
|
// so we can verify whether they have changed or not.
|
|
|
|
FilesInPreamble.clear();
|
2011-03-22 02:40:07 +08:00
|
|
|
SourceManager &SourceMgr = Clang->getSourceManager();
|
2010-07-31 08:40:00 +08:00
|
|
|
const llvm::MemoryBuffer *MainFileBuffer
|
|
|
|
= SourceMgr.getBuffer(SourceMgr.getMainFileID());
|
|
|
|
for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
|
|
|
|
FEnd = SourceMgr.fileinfo_end();
|
|
|
|
F != FEnd;
|
|
|
|
++F) {
|
2011-03-05 09:03:53 +08:00
|
|
|
const FileEntry *File = F->second->OrigEntry;
|
2013-12-20 08:16:25 +08:00
|
|
|
if (!File)
|
2010-07-31 08:40:00 +08:00
|
|
|
continue;
|
2013-12-20 08:16:25 +08:00
|
|
|
const llvm::MemoryBuffer *Buffer = F->second->getRawBuffer();
|
|
|
|
if (Buffer == MainFileBuffer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (time_t ModTime = File->getModificationTime()) {
|
|
|
|
FilesInPreamble[File->getName()] = PreambleFileHash::createForFile(
|
|
|
|
F->second->getSize(), ModTime);
|
|
|
|
} else {
|
|
|
|
assert(F->second->getSize() == Buffer->getBufferSize());
|
|
|
|
FilesInPreamble[File->getName()] =
|
|
|
|
PreambleFileHash::createForMemoryBuffer(Buffer);
|
|
|
|
}
|
2010-07-31 08:40:00 +08:00
|
|
|
}
|
|
|
|
|
2010-08-04 13:53:38 +08:00
|
|
|
PreambleRebuildCounter = 1;
|
2010-08-19 09:33:06 +08:00
|
|
|
PreprocessorOpts.eraseRemappedFile(
|
|
|
|
PreprocessorOpts.remapped_file_buffer_end() - 1);
|
2011-02-17 02:16:54 +08:00
|
|
|
|
|
|
|
// If the hash of top-level entities differs from the hash of the top-level
|
|
|
|
// entities the last time we rebuilt the preamble, clear out the completion
|
|
|
|
// cache.
|
|
|
|
if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
|
|
|
|
CompletionCacheTopLevelHashValue = 0;
|
|
|
|
PreambleTopLevelHashValue = CurrentTopLevelHashValue;
|
|
|
|
}
|
|
|
|
|
2010-07-24 08:38:13 +08:00
|
|
|
return CreatePaddedMainFileBuffer(NewPreamble.first,
|
|
|
|
PreambleReservedSize,
|
2012-11-10 03:40:39 +08:00
|
|
|
FrontendOpts.Inputs[0].getFile());
|
2010-07-23 08:33:23 +08:00
|
|
|
}
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2010-08-04 03:06:41 +08:00
|
|
|
void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
|
|
|
|
std::vector<Decl *> Resolved;
|
|
|
|
Resolved.reserve(TopLevelDeclsInPreamble.size());
|
|
|
|
ExternalASTSource &Source = *getASTContext().getExternalSource();
|
|
|
|
for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
|
|
|
|
// Resolve the declaration ID to an actual declaration, possibly
|
|
|
|
// deserializing the declaration in the process.
|
|
|
|
Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
|
|
|
|
if (D)
|
|
|
|
Resolved.push_back(D);
|
|
|
|
}
|
|
|
|
TopLevelDeclsInPreamble.clear();
|
|
|
|
TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
|
|
|
|
}
|
|
|
|
|
2012-04-11 10:11:16 +08:00
|
|
|
void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
|
|
|
|
// Steal the created target, context, and preprocessor.
|
|
|
|
TheSema.reset(CI.takeSema());
|
|
|
|
Consumer.reset(CI.takeASTConsumer());
|
|
|
|
Ctx = &CI.getASTContext();
|
|
|
|
PP = &CI.getPreprocessor();
|
|
|
|
CI.setSourceManager(0);
|
|
|
|
CI.setFileManager(0);
|
|
|
|
Target = &CI.getTarget();
|
|
|
|
Reader = CI.getModuleManager();
|
2013-05-24 13:44:08 +08:00
|
|
|
HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
|
2012-04-11 10:11:16 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef ASTUnit::getMainFileName() const {
|
2013-01-12 06:11:14 +08:00
|
|
|
if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
|
|
|
|
const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
|
|
|
|
if (Input.isFile())
|
|
|
|
return Input.getFile();
|
|
|
|
else
|
|
|
|
return Input.getBuffer()->getBufferIdentifier();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SourceMgr) {
|
|
|
|
if (const FileEntry *
|
|
|
|
FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
|
|
|
|
return FE->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return StringRef();
|
2010-10-28 23:44:59 +08:00
|
|
|
}
|
|
|
|
|
2013-03-06 04:21:14 +08:00
|
|
|
StringRef ASTUnit::getASTFileName() const {
|
|
|
|
if (!isMainFileAST())
|
|
|
|
return StringRef();
|
|
|
|
|
|
|
|
serialization::ModuleFile &
|
|
|
|
Mod = Reader->getModuleManager().getPrimaryModule();
|
|
|
|
return Mod.FileName;
|
|
|
|
}
|
|
|
|
|
2011-03-10 01:21:42 +08:00
|
|
|
ASTUnit *ASTUnit::create(CompilerInvocation *CI,
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
|
2012-07-12 04:59:04 +08:00
|
|
|
bool CaptureDiagnostics,
|
|
|
|
bool UserFilesAreVolatile) {
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ASTUnit> AST;
|
2011-03-10 01:21:42 +08:00
|
|
|
AST.reset(new ASTUnit(false));
|
2011-11-28 12:55:55 +08:00
|
|
|
ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
|
2011-03-10 01:21:42 +08:00
|
|
|
AST->Diagnostics = Diags;
|
2011-03-22 02:40:17 +08:00
|
|
|
AST->Invocation = CI;
|
2011-03-19 02:22:40 +08:00
|
|
|
AST->FileSystemOpts = CI->getFileSystemOpts();
|
2011-03-22 02:40:17 +08:00
|
|
|
AST->FileMgr = new FileManager(AST->FileSystemOpts);
|
2012-07-12 04:59:04 +08:00
|
|
|
AST->UserFilesAreVolatile = UserFilesAreVolatile;
|
|
|
|
AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
|
|
|
|
UserFilesAreVolatile);
|
2011-03-10 01:21:42 +08:00
|
|
|
|
|
|
|
return AST.take();
|
|
|
|
}
|
|
|
|
|
2011-05-04 07:26:34 +08:00
|
|
|
ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
|
2011-10-15 05:22:05 +08:00
|
|
|
ASTFrontendAction *Action,
|
2011-11-28 12:56:00 +08:00
|
|
|
ASTUnit *Unit,
|
|
|
|
bool Persistent,
|
|
|
|
StringRef ResourceFilesPath,
|
|
|
|
bool OnlyLocalDecls,
|
|
|
|
bool CaptureDiagnostics,
|
|
|
|
bool PrecompilePreamble,
|
2012-04-11 10:11:16 +08:00
|
|
|
bool CacheCodeCompletionResults,
|
2012-07-03 01:35:10 +08:00
|
|
|
bool IncludeBriefCommentsInCodeCompletion,
|
2012-07-12 04:59:04 +08:00
|
|
|
bool UserFilesAreVolatile,
|
2012-04-11 10:11:16 +08:00
|
|
|
OwningPtr<ASTUnit> *ErrAST) {
|
2011-05-04 07:26:34 +08:00
|
|
|
assert(CI && "A CompilerInvocation is required");
|
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ASTUnit> OwnAST;
|
2011-10-15 05:22:05 +08:00
|
|
|
ASTUnit *AST = Unit;
|
|
|
|
if (!AST) {
|
|
|
|
// Create the AST unit.
|
2012-07-12 04:59:04 +08:00
|
|
|
OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
|
2011-10-15 05:22:05 +08:00
|
|
|
AST = OwnAST.get();
|
|
|
|
}
|
|
|
|
|
2011-11-28 12:56:00 +08:00
|
|
|
if (!ResourceFilesPath.empty()) {
|
|
|
|
// Override the resources path.
|
|
|
|
CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
|
|
|
|
}
|
|
|
|
AST->OnlyLocalDecls = OnlyLocalDecls;
|
|
|
|
AST->CaptureDiagnostics = CaptureDiagnostics;
|
|
|
|
if (PrecompilePreamble)
|
|
|
|
AST->PreambleRebuildCounter = 2;
|
2011-08-26 06:30:56 +08:00
|
|
|
AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
|
2011-11-28 12:56:00 +08:00
|
|
|
AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
|
2012-07-03 01:35:10 +08:00
|
|
|
AST->IncludeBriefCommentsInCodeCompletion
|
|
|
|
= IncludeBriefCommentsInCodeCompletion;
|
2011-05-04 07:26:34 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
|
2011-10-15 05:22:05 +08:00
|
|
|
ASTUnitCleanup(OwnAST.get());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
|
|
|
|
llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
|
2011-05-04 07:26:34 +08:00
|
|
|
DiagCleanup(Diags.getPtr());
|
|
|
|
|
|
|
|
// We'll manage file buffers ourselves.
|
|
|
|
CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
|
|
|
|
CI->getFrontendOpts().DisableFree = false;
|
|
|
|
ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
|
|
|
|
|
|
|
|
// Create the compiler instance to use for building the AST.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<CompilerInstance> Clang(new CompilerInstance());
|
2011-05-04 07:26:34 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
|
|
|
|
CICleanup(Clang.get());
|
|
|
|
|
|
|
|
Clang->setInvocation(CI);
|
2012-11-10 03:40:39 +08:00
|
|
|
AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
|
2011-05-04 07:26:34 +08:00
|
|
|
|
|
|
|
// Set up diagnostics, capturing any diagnostics that would
|
|
|
|
// otherwise be dropped.
|
|
|
|
Clang->setDiagnostics(&AST->getDiagnostics());
|
|
|
|
|
|
|
|
// Create the target instance.
|
|
|
|
Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
|
2012-11-16 12:24:59 +08:00
|
|
|
&Clang->getTargetOpts()));
|
2011-05-04 07:26:34 +08:00
|
|
|
if (!Clang->hasTarget())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Inform the target of the language options.
|
|
|
|
//
|
|
|
|
// FIXME: We shouldn't need to do this, the target should be immutable once
|
|
|
|
// created. This complexity should be lifted elsewhere.
|
|
|
|
Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
|
|
|
|
|
|
|
|
assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
|
|
|
|
"Invocation must have exactly one source file!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
|
2011-05-04 07:26:34 +08:00
|
|
|
"FIXME: AST inputs not yet supported here!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
|
2011-05-04 07:26:34 +08:00
|
|
|
"IR inputs not supported here!");
|
|
|
|
|
|
|
|
// Configure the various subsystems.
|
|
|
|
AST->TheSema.reset();
|
|
|
|
AST->Ctx = 0;
|
|
|
|
AST->PP = 0;
|
2011-11-02 01:14:15 +08:00
|
|
|
AST->Reader = 0;
|
2011-05-04 07:26:34 +08:00
|
|
|
|
|
|
|
// Create a file manager object to provide access to and cache the filesystem.
|
|
|
|
Clang->setFileManager(&AST->getFileManager());
|
|
|
|
|
|
|
|
// Create the source manager.
|
|
|
|
Clang->setSourceManager(&AST->getSourceManager());
|
|
|
|
|
|
|
|
ASTFrontendAction *Act = Action;
|
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
|
2011-05-04 07:26:34 +08:00
|
|
|
if (!Act) {
|
|
|
|
TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
|
|
|
|
Act = TrackerAct.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
|
|
|
|
ActCleanup(TrackerAct.get());
|
|
|
|
|
2012-04-11 10:11:16 +08:00
|
|
|
if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
|
|
|
|
AST->transferASTDataFromCompilerInstance(*Clang);
|
|
|
|
if (OwnAST && ErrAST)
|
|
|
|
ErrAST->swap(OwnAST);
|
|
|
|
|
2011-05-04 07:26:34 +08:00
|
|
|
return 0;
|
2012-04-11 10:11:16 +08:00
|
|
|
}
|
2011-11-28 12:56:00 +08:00
|
|
|
|
|
|
|
if (Persistent && !TrackerAct) {
|
|
|
|
Clang->getPreprocessor().addPPCallbacks(
|
|
|
|
new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue()));
|
|
|
|
std::vector<ASTConsumer*> Consumers;
|
|
|
|
if (Clang->hasASTConsumer())
|
|
|
|
Consumers.push_back(Clang->takeASTConsumer());
|
|
|
|
Consumers.push_back(new TopLevelDeclTrackerConsumer(*AST,
|
|
|
|
AST->getCurrentTopLevelHashValue()));
|
|
|
|
Clang->setASTConsumer(new MultiplexConsumer(Consumers));
|
|
|
|
}
|
2012-06-08 13:48:06 +08:00
|
|
|
if (!Act->Execute()) {
|
|
|
|
AST->transferASTDataFromCompilerInstance(*Clang);
|
|
|
|
if (OwnAST && ErrAST)
|
|
|
|
ErrAST->swap(OwnAST);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-11 10:11:16 +08:00
|
|
|
|
2011-05-04 07:26:34 +08:00
|
|
|
// Steal the created target, context, and preprocessor.
|
2012-04-11 10:11:16 +08:00
|
|
|
AST->transferASTDataFromCompilerInstance(*Clang);
|
2011-05-04 07:26:34 +08:00
|
|
|
|
|
|
|
Act->EndSourceFile();
|
|
|
|
|
2011-10-15 05:22:05 +08:00
|
|
|
if (OwnAST)
|
|
|
|
return OwnAST.take();
|
|
|
|
else
|
|
|
|
return AST;
|
2011-05-04 07:26:34 +08:00
|
|
|
}
|
|
|
|
|
2010-10-12 08:50:20 +08:00
|
|
|
bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
|
|
|
|
if (!Invocation)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We'll manage file buffers ourselves.
|
|
|
|
Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
|
|
|
|
Invocation->getFrontendOpts().DisableFree = false;
|
2011-01-19 09:02:47 +08:00
|
|
|
ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
|
2010-10-12 08:50:20 +08:00
|
|
|
|
|
|
|
llvm::MemoryBuffer *OverrideMainBuffer = 0;
|
2010-10-28 01:24:53 +08:00
|
|
|
if (PrecompilePreamble) {
|
2010-11-16 07:00:34 +08:00
|
|
|
PreambleRebuildCounter = 2;
|
2010-10-12 08:50:20 +08:00
|
|
|
OverrideMainBuffer
|
|
|
|
= getMainBufferWithPrecompiledPreamble(*Invocation);
|
|
|
|
}
|
|
|
|
|
2010-10-28 23:44:59 +08:00
|
|
|
SimpleTimer ParsingTimer(WantTiming);
|
2010-11-10 04:00:56 +08:00
|
|
|
ParsingTimer.setOutput("Parsing " + getMainFileName());
|
2010-10-12 08:50:20 +08:00
|
|
|
|
2011-03-22 09:15:24 +08:00
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
|
|
|
|
MemBufferCleanup(OverrideMainBuffer);
|
|
|
|
|
2010-10-28 23:44:59 +08:00
|
|
|
return Parse(OverrideMainBuffer);
|
2010-10-12 08:50:20 +08:00
|
|
|
}
|
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
|
2010-07-20 05:46:24 +08:00
|
|
|
bool OnlyLocalDecls,
|
2010-07-23 08:33:23 +08:00
|
|
|
bool CaptureDiagnostics,
|
2010-08-10 04:45:32 +08:00
|
|
|
bool PrecompilePreamble,
|
2011-08-26 06:30:56 +08:00
|
|
|
TranslationUnitKind TUKind,
|
2012-07-03 01:35:10 +08:00
|
|
|
bool CacheCodeCompletionResults,
|
2012-07-12 04:59:04 +08:00
|
|
|
bool IncludeBriefCommentsInCodeCompletion,
|
|
|
|
bool UserFilesAreVolatile) {
|
2010-07-20 05:46:24 +08:00
|
|
|
// Create the AST unit.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ASTUnit> AST;
|
2010-07-20 05:46:24 +08:00
|
|
|
AST.reset(new ASTUnit(false));
|
2011-01-19 09:02:47 +08:00
|
|
|
ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
|
2010-07-20 05:46:24 +08:00
|
|
|
AST->Diagnostics = Diags;
|
|
|
|
AST->OnlyLocalDecls = OnlyLocalDecls;
|
2010-11-11 08:39:14 +08:00
|
|
|
AST->CaptureDiagnostics = CaptureDiagnostics;
|
2011-08-26 06:30:56 +08:00
|
|
|
AST->TUKind = TUKind;
|
2010-08-14 06:48:40 +08:00
|
|
|
AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
|
2012-07-03 01:35:10 +08:00
|
|
|
AST->IncludeBriefCommentsInCodeCompletion
|
|
|
|
= IncludeBriefCommentsInCodeCompletion;
|
2011-03-22 02:40:17 +08:00
|
|
|
AST->Invocation = CI;
|
2013-01-22 02:45:42 +08:00
|
|
|
AST->FileSystemOpts = CI->getFileSystemOpts();
|
|
|
|
AST->FileMgr = new FileManager(AST->FileSystemOpts);
|
2012-07-12 04:59:04 +08:00
|
|
|
AST->UserFilesAreVolatile = UserFilesAreVolatile;
|
2010-07-31 04:58:08 +08:00
|
|
|
|
2011-03-18 10:06:56 +08:00
|
|
|
// Recover resources if we crash before exiting this method.
|
2011-03-22 09:15:24 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
|
|
|
|
ASTUnitCleanup(AST.get());
|
2011-09-26 07:23:43 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
|
|
|
|
llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
|
2011-03-22 09:15:24 +08:00
|
|
|
DiagCleanup(Diags.getPtr());
|
2011-03-18 10:06:56 +08:00
|
|
|
|
2010-10-12 08:50:20 +08:00
|
|
|
return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
|
2009-12-01 17:51:01 +08:00
|
|
|
}
|
2009-12-02 11:23:45 +08:00
|
|
|
|
|
|
|
ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
|
|
|
|
const char **ArgEnd,
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef ResourceFilesPath,
|
2009-12-02 11:23:45 +08:00
|
|
|
bool OnlyLocalDecls,
|
2010-11-11 08:39:14 +08:00
|
|
|
bool CaptureDiagnostics,
|
2014-02-07 23:00:22 +08:00
|
|
|
ArrayRef<RemappedFile> RemappedFiles,
|
2011-03-09 07:35:24 +08:00
|
|
|
bool RemappedFilesKeepOriginalName,
|
2010-08-10 04:45:32 +08:00
|
|
|
bool PrecompilePreamble,
|
2011-08-26 06:30:56 +08:00
|
|
|
TranslationUnitKind TUKind,
|
2012-03-07 09:51:17 +08:00
|
|
|
bool CacheCodeCompletionResults,
|
2012-07-03 01:35:10 +08:00
|
|
|
bool IncludeBriefCommentsInCodeCompletion,
|
2012-04-11 10:11:16 +08:00
|
|
|
bool AllowPCHWithCompilerErrors,
|
2012-04-12 18:11:59 +08:00
|
|
|
bool SkipFunctionBodies,
|
2012-07-12 04:59:04 +08:00
|
|
|
bool UserFilesAreVolatile,
|
2012-10-12 00:05:00 +08:00
|
|
|
bool ForSerialization,
|
2012-04-11 10:11:16 +08:00
|
|
|
OwningPtr<ASTUnit> *ErrAST) {
|
2010-04-06 07:52:57 +08:00
|
|
|
if (!Diags.getPtr()) {
|
2010-04-06 05:10:19 +08:00
|
|
|
// No diagnostics engine was provided, so create our own diagnostics object
|
|
|
|
// with the default options.
|
2013-01-20 09:58:28 +08:00
|
|
|
Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions());
|
2010-04-06 05:10:19 +08:00
|
|
|
}
|
2009-12-02 11:23:45 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
|
2010-10-12 08:50:20 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<CompilerInvocation> CI;
|
2010-11-11 08:39:14 +08:00
|
|
|
|
2010-10-12 08:50:20 +08:00
|
|
|
{
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
|
2010-11-11 08:39:14 +08:00
|
|
|
CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
|
2010-10-12 08:50:20 +08:00
|
|
|
StoredDiagnostics);
|
|
|
|
|
2011-04-05 07:11:45 +08:00
|
|
|
CI = clang::createInvocationFromCommandLine(
|
2011-07-18 20:00:32 +08:00
|
|
|
llvm::makeArrayRef(ArgBegin, ArgEnd),
|
|
|
|
Diags);
|
2011-04-05 05:38:51 +08:00
|
|
|
if (!CI)
|
2011-03-08 06:45:01 +08:00
|
|
|
return 0;
|
2009-12-02 11:23:45 +08:00
|
|
|
}
|
2010-11-11 08:39:14 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
// Override any files that need remapping
|
2014-02-07 23:00:22 +08:00
|
|
|
for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
|
2014-02-08 08:38:15 +08:00
|
|
|
CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
|
|
|
|
RemappedFiles[I].second);
|
2011-03-05 09:03:53 +08:00
|
|
|
}
|
2012-03-07 09:51:17 +08:00
|
|
|
PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
|
|
|
|
PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
|
|
|
|
PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
|
2010-01-23 08:14:00 +08:00
|
|
|
|
2009-12-15 08:06:45 +08:00
|
|
|
// Override the resources path.
|
2010-01-31 05:47:16 +08:00
|
|
|
CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
|
2009-12-02 11:23:45 +08:00
|
|
|
|
2012-04-12 18:11:59 +08:00
|
|
|
CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
|
|
|
|
|
2010-10-12 08:50:20 +08:00
|
|
|
// Create the AST unit.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ASTUnit> AST;
|
2010-10-12 08:50:20 +08:00
|
|
|
AST.reset(new ASTUnit(false));
|
2011-01-19 09:02:47 +08:00
|
|
|
ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
|
2010-10-12 08:50:20 +08:00
|
|
|
AST->Diagnostics = Diags;
|
2011-11-18 07:01:17 +08:00
|
|
|
Diags = 0; // Zero out now to ease cleanup during crash recovery.
|
2011-03-19 02:22:40 +08:00
|
|
|
AST->FileSystemOpts = CI->getFileSystemOpts();
|
2011-03-22 02:40:17 +08:00
|
|
|
AST->FileMgr = new FileManager(AST->FileSystemOpts);
|
2010-10-12 08:50:20 +08:00
|
|
|
AST->OnlyLocalDecls = OnlyLocalDecls;
|
2010-11-11 08:39:14 +08:00
|
|
|
AST->CaptureDiagnostics = CaptureDiagnostics;
|
2011-08-26 06:30:56 +08:00
|
|
|
AST->TUKind = TUKind;
|
2010-10-12 08:50:20 +08:00
|
|
|
AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
|
2012-07-03 01:35:10 +08:00
|
|
|
AST->IncludeBriefCommentsInCodeCompletion
|
|
|
|
= IncludeBriefCommentsInCodeCompletion;
|
2012-07-12 04:59:04 +08:00
|
|
|
AST->UserFilesAreVolatile = UserFilesAreVolatile;
|
2010-10-12 08:50:20 +08:00
|
|
|
AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
|
|
|
|
AST->StoredDiagnostics.swap(StoredDiagnostics);
|
2011-03-22 02:40:17 +08:00
|
|
|
AST->Invocation = CI;
|
2012-10-12 00:05:00 +08:00
|
|
|
if (ForSerialization)
|
|
|
|
AST->WriterData.reset(new ASTWriterData());
|
2011-11-18 07:01:17 +08:00
|
|
|
CI = 0; // Zero out now to ease cleanup during crash recovery.
|
2011-03-18 10:06:56 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2011-03-22 09:15:24 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
|
|
|
|
ASTUnitCleanup(AST.get());
|
2011-03-18 10:06:56 +08:00
|
|
|
|
2012-04-11 10:11:16 +08:00
|
|
|
if (AST->LoadFromCompilerInvocation(PrecompilePreamble)) {
|
|
|
|
// Some error occurred, if caller wants to examine diagnostics, pass it the
|
|
|
|
// ASTUnit.
|
|
|
|
if (ErrAST) {
|
|
|
|
AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
|
|
|
|
ErrAST->swap(AST);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AST.take();
|
2009-12-02 11:23:45 +08:00
|
|
|
}
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2014-02-07 23:00:22 +08:00
|
|
|
bool ASTUnit::Reparse(ArrayRef<RemappedFile> RemappedFiles) {
|
2011-03-22 02:40:17 +08:00
|
|
|
if (!Invocation)
|
2010-07-20 05:46:24 +08:00
|
|
|
return true;
|
2011-10-31 15:19:59 +08:00
|
|
|
|
|
|
|
clearFileLevelDecls();
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2010-10-28 23:44:59 +08:00
|
|
|
SimpleTimer ParsingTimer(WantTiming);
|
2010-11-10 04:00:56 +08:00
|
|
|
ParsingTimer.setOutput("Reparsing " + getMainFileName());
|
2010-10-28 23:44:59 +08:00
|
|
|
|
2010-07-31 08:40:00 +08:00
|
|
|
// Remap files.
|
2010-08-20 08:02:33 +08:00
|
|
|
PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
|
|
|
|
for (PreprocessorOptions::remapped_file_buffer_iterator
|
|
|
|
R = PPOpts.remapped_file_buffer_begin(),
|
|
|
|
REnd = PPOpts.remapped_file_buffer_end();
|
|
|
|
R != REnd;
|
|
|
|
++R) {
|
|
|
|
delete R->second;
|
|
|
|
}
|
2010-07-31 08:40:00 +08:00
|
|
|
Invocation->getPreprocessorOpts().clearRemappedFiles();
|
2014-02-07 23:00:22 +08:00
|
|
|
for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
|
2014-02-08 08:38:15 +08:00
|
|
|
Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
|
|
|
|
RemappedFiles[I].second);
|
2011-03-05 09:03:53 +08:00
|
|
|
}
|
2014-02-08 08:38:15 +08:00
|
|
|
|
2010-08-04 13:53:38 +08:00
|
|
|
// If we have a preamble file lying around, or if we might try to
|
|
|
|
// build a precompiled preamble, do so now.
|
2010-07-24 08:38:13 +08:00
|
|
|
llvm::MemoryBuffer *OverrideMainBuffer = 0;
|
2011-10-28 01:55:18 +08:00
|
|
|
if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
|
2010-08-20 08:59:43 +08:00
|
|
|
OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
|
2010-07-24 07:58:40 +08:00
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
// Clear out the diagnostics state.
|
2011-11-04 04:28:19 +08:00
|
|
|
getDiagnostics().Reset();
|
|
|
|
ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
|
2011-11-04 04:57:33 +08:00
|
|
|
if (OverrideMainBuffer)
|
|
|
|
getDiagnostics().setNumWarnings(NumWarningsInPreamble);
|
2011-11-04 04:28:19 +08:00
|
|
|
|
2010-07-24 07:58:40 +08:00
|
|
|
// Parse the sources
|
2011-02-17 02:16:54 +08:00
|
|
|
bool Result = Parse(OverrideMainBuffer);
|
2011-11-01 05:25:31 +08:00
|
|
|
|
|
|
|
// If we're caching global code-completion results, and the top-level
|
|
|
|
// declarations have changed, clear out the code-completion cache.
|
|
|
|
if (!Result && ShouldCacheCodeCompletionResults &&
|
|
|
|
CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
|
|
|
|
CacheCodeCompletionResults();
|
2011-02-17 02:16:54 +08:00
|
|
|
|
2012-04-11 01:23:48 +08:00
|
|
|
// We now need to clear out the completion info related to this translation
|
|
|
|
// unit; it'll be recreated if necessary.
|
|
|
|
CCTUInfo.reset();
|
2011-08-05 04:04:59 +08:00
|
|
|
|
2010-07-24 07:58:40 +08:00
|
|
|
return Result;
|
2010-07-20 05:46:24 +08:00
|
|
|
}
|
2010-08-05 00:47:14 +08:00
|
|
|
|
2010-08-14 06:48:40 +08:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// Code completion
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// \brief Code completion consumer that combines the cached code-completion
|
|
|
|
/// results from an ASTUnit with the code-completion results provided to it,
|
|
|
|
/// then passes the result on to
|
|
|
|
class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
|
2012-08-14 11:13:00 +08:00
|
|
|
uint64_t NormalContexts;
|
2010-08-14 06:48:40 +08:00
|
|
|
ASTUnit &AST;
|
|
|
|
CodeCompleteConsumer &Next;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
|
2012-07-03 01:35:10 +08:00
|
|
|
const CodeCompleteOptions &CodeCompleteOpts)
|
|
|
|
: CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
|
|
|
|
AST(AST), Next(Next)
|
2010-08-14 06:48:40 +08:00
|
|
|
{
|
|
|
|
// Compute the set of contexts in which we will look when we don't have
|
|
|
|
// any information about the specific context.
|
|
|
|
NormalContexts
|
2012-08-14 11:13:00 +08:00
|
|
|
= (1LL << CodeCompletionContext::CCC_TopLevel)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCInterface)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCImplementation)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCIvarList)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Statement)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Expression)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_DotMemberAccess)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_Recovery);
|
2010-09-15 07:59:36 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (AST.getASTContext().getLangOpts().CPlusPlus)
|
2012-08-14 11:13:00 +08:00
|
|
|
NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_UnionTag)
|
|
|
|
| (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
|
2010-08-14 06:48:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ProcessCodeCompleteResults(Sema &S,
|
|
|
|
CodeCompletionContext Context,
|
2010-08-25 14:19:51 +08:00
|
|
|
CodeCompletionResult *Results,
|
2010-08-17 04:01:48 +08:00
|
|
|
unsigned NumResults);
|
2010-08-14 06:48:40 +08:00
|
|
|
|
|
|
|
virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
|
|
|
|
OverloadCandidate *Candidates,
|
|
|
|
unsigned NumCandidates) {
|
|
|
|
Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
|
|
|
|
}
|
2011-02-02 03:23:04 +08:00
|
|
|
|
2011-02-02 06:57:45 +08:00
|
|
|
virtual CodeCompletionAllocator &getAllocator() {
|
2011-02-02 03:23:04 +08:00
|
|
|
return Next.getAllocator();
|
|
|
|
}
|
2012-04-11 01:23:48 +08:00
|
|
|
|
|
|
|
virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() {
|
|
|
|
return Next.getCodeCompletionTUInfo();
|
|
|
|
}
|
2010-08-14 06:48:40 +08:00
|
|
|
};
|
|
|
|
}
|
2010-08-17 04:01:48 +08:00
|
|
|
|
2010-08-17 05:18:39 +08:00
|
|
|
/// \brief Helper function that computes which global names are hidden by the
|
|
|
|
/// local code-completion results.
|
2010-11-07 14:11:36 +08:00
|
|
|
static void CalculateHiddenNames(const CodeCompletionContext &Context,
|
|
|
|
CodeCompletionResult *Results,
|
|
|
|
unsigned NumResults,
|
|
|
|
ASTContext &Ctx,
|
|
|
|
llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
|
2010-08-17 05:18:39 +08:00
|
|
|
bool OnlyTagNames = false;
|
|
|
|
switch (Context.getKind()) {
|
2010-09-24 07:01:17 +08:00
|
|
|
case CodeCompletionContext::CCC_Recovery:
|
2010-08-17 05:18:39 +08:00
|
|
|
case CodeCompletionContext::CCC_TopLevel:
|
|
|
|
case CodeCompletionContext::CCC_ObjCInterface:
|
|
|
|
case CodeCompletionContext::CCC_ObjCImplementation:
|
|
|
|
case CodeCompletionContext::CCC_ObjCIvarList:
|
|
|
|
case CodeCompletionContext::CCC_ClassStructUnion:
|
|
|
|
case CodeCompletionContext::CCC_Statement:
|
|
|
|
case CodeCompletionContext::CCC_Expression:
|
|
|
|
case CodeCompletionContext::CCC_ObjCMessageReceiver:
|
2011-07-08 00:03:39 +08:00
|
|
|
case CodeCompletionContext::CCC_DotMemberAccess:
|
|
|
|
case CodeCompletionContext::CCC_ArrowMemberAccess:
|
|
|
|
case CodeCompletionContext::CCC_ObjCPropertyAccess:
|
2010-08-17 05:18:39 +08:00
|
|
|
case CodeCompletionContext::CCC_Namespace:
|
|
|
|
case CodeCompletionContext::CCC_Type:
|
2010-08-24 02:23:48 +08:00
|
|
|
case CodeCompletionContext::CCC_Name:
|
|
|
|
case CodeCompletionContext::CCC_PotentiallyQualifiedName:
|
2010-09-15 07:59:36 +08:00
|
|
|
case CodeCompletionContext::CCC_ParenthesizedExpression:
|
2011-07-30 14:55:39 +08:00
|
|
|
case CodeCompletionContext::CCC_ObjCInterfaceName:
|
2010-08-17 05:18:39 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CodeCompletionContext::CCC_EnumTag:
|
|
|
|
case CodeCompletionContext::CCC_UnionTag:
|
|
|
|
case CodeCompletionContext::CCC_ClassOrStructTag:
|
|
|
|
OnlyTagNames = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CodeCompletionContext::CCC_ObjCProtocolName:
|
2010-08-25 04:21:13 +08:00
|
|
|
case CodeCompletionContext::CCC_MacroName:
|
|
|
|
case CodeCompletionContext::CCC_MacroNameUse:
|
2010-08-25 06:20:20 +08:00
|
|
|
case CodeCompletionContext::CCC_PreprocessorExpression:
|
2010-08-26 02:41:16 +08:00
|
|
|
case CodeCompletionContext::CCC_PreprocessorDirective:
|
2010-08-26 02:04:30 +08:00
|
|
|
case CodeCompletionContext::CCC_NaturalLanguage:
|
2010-08-26 23:07:07 +08:00
|
|
|
case CodeCompletionContext::CCC_SelectorName:
|
2010-08-28 01:35:51 +08:00
|
|
|
case CodeCompletionContext::CCC_TypeQualifiers:
|
2010-09-24 07:01:17 +08:00
|
|
|
case CodeCompletionContext::CCC_Other:
|
2011-02-19 07:30:37 +08:00
|
|
|
case CodeCompletionContext::CCC_OtherWithMacros:
|
2011-07-08 00:03:39 +08:00
|
|
|
case CodeCompletionContext::CCC_ObjCInstanceMessage:
|
|
|
|
case CodeCompletionContext::CCC_ObjCClassMessage:
|
|
|
|
case CodeCompletionContext::CCC_ObjCCategoryName:
|
2010-08-26 02:41:16 +08:00
|
|
|
// We're looking for nothing, or we're looking for names that cannot
|
|
|
|
// be hidden.
|
2010-08-17 05:18:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-25 14:19:51 +08:00
|
|
|
typedef CodeCompletionResult Result;
|
2010-08-17 05:18:39 +08:00
|
|
|
for (unsigned I = 0; I != NumResults; ++I) {
|
|
|
|
if (Results[I].Kind != Result::RK_Declaration)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned IDNS
|
|
|
|
= Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
|
|
|
|
|
|
|
|
bool Hiding = false;
|
|
|
|
if (OnlyTagNames)
|
|
|
|
Hiding = (IDNS & Decl::IDNS_Tag);
|
|
|
|
else {
|
|
|
|
unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
|
2010-08-17 07:05:20 +08:00
|
|
|
Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
|
|
|
|
Decl::IDNS_NonMemberOperator);
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Ctx.getLangOpts().CPlusPlus)
|
2010-08-17 05:18:39 +08:00
|
|
|
HiddenIDNS |= Decl::IDNS_Tag;
|
|
|
|
Hiding = (IDNS & HiddenIDNS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Hiding)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DeclarationName Name = Results[I].Declaration->getDeclName();
|
|
|
|
if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
|
|
|
|
HiddenNames.insert(Identifier->getName());
|
|
|
|
else
|
|
|
|
HiddenNames.insert(Name.getAsString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-17 04:01:48 +08:00
|
|
|
void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
|
|
|
|
CodeCompletionContext Context,
|
2010-08-25 14:19:51 +08:00
|
|
|
CodeCompletionResult *Results,
|
2010-08-17 04:01:48 +08:00
|
|
|
unsigned NumResults) {
|
|
|
|
// Merge the results we were given with the results we cached.
|
|
|
|
bool AddedResult = false;
|
2012-08-14 11:13:00 +08:00
|
|
|
uint64_t InContexts =
|
|
|
|
Context.getKind() == CodeCompletionContext::CCC_Recovery
|
|
|
|
? NormalContexts : (1LL << Context.getKind());
|
2010-08-17 05:18:39 +08:00
|
|
|
// Contains the set of names that are hidden by "local" completion results.
|
2010-11-07 14:11:36 +08:00
|
|
|
llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
|
2010-08-25 14:19:51 +08:00
|
|
|
typedef CodeCompletionResult Result;
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Result, 8> AllResults;
|
2010-08-17 04:01:48 +08:00
|
|
|
for (ASTUnit::cached_completion_iterator
|
2010-08-17 05:23:13 +08:00
|
|
|
C = AST.cached_completion_begin(),
|
|
|
|
CEnd = AST.cached_completion_end();
|
2010-08-17 04:01:48 +08:00
|
|
|
C != CEnd; ++C) {
|
|
|
|
// If the context we are in matches any of the contexts we are
|
|
|
|
// interested in, we'll add this result.
|
|
|
|
if ((C->ShowInContexts & InContexts) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If we haven't added any results previously, do so now.
|
|
|
|
if (!AddedResult) {
|
2010-08-17 05:18:39 +08:00
|
|
|
CalculateHiddenNames(Context, Results, NumResults, S.Context,
|
|
|
|
HiddenNames);
|
2010-08-17 04:01:48 +08:00
|
|
|
AllResults.insert(AllResults.end(), Results, Results + NumResults);
|
|
|
|
AddedResult = true;
|
|
|
|
}
|
|
|
|
|
2010-08-17 05:18:39 +08:00
|
|
|
// Determine whether this global completion result is hidden by a local
|
|
|
|
// completion result. If so, skip it.
|
|
|
|
if (C->Kind != CXCursor_MacroDefinition &&
|
|
|
|
HiddenNames.count(C->Completion->getTypedText()))
|
|
|
|
continue;
|
|
|
|
|
2010-08-17 04:01:48 +08:00
|
|
|
// Adjust priority based on similar type classes.
|
|
|
|
unsigned Priority = C->Priority;
|
2010-08-25 04:21:13 +08:00
|
|
|
CodeCompletionString *Completion = C->Completion;
|
2010-08-17 04:01:48 +08:00
|
|
|
if (!Context.getPreferredType().isNull()) {
|
|
|
|
if (C->Kind == CXCursor_MacroDefinition) {
|
|
|
|
Priority = getMacroUsagePriority(C->Completion->getTypedText(),
|
2012-03-11 15:00:24 +08:00
|
|
|
S.getLangOpts(),
|
2010-08-25 04:21:13 +08:00
|
|
|
Context.getPreferredType()->isAnyPointerType());
|
2010-08-17 04:01:48 +08:00
|
|
|
} else if (C->Type) {
|
|
|
|
CanQualType Expected
|
2010-08-17 05:23:13 +08:00
|
|
|
= S.Context.getCanonicalType(
|
2010-08-17 04:01:48 +08:00
|
|
|
Context.getPreferredType().getUnqualifiedType());
|
|
|
|
SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
|
|
|
|
if (ExpectedSTC == C->TypeClass) {
|
|
|
|
// We know this type is similar; check for an exact match.
|
|
|
|
llvm::StringMap<unsigned> &CachedCompletionTypes
|
2010-08-17 05:23:13 +08:00
|
|
|
= AST.getCachedCompletionTypes();
|
2010-08-17 04:01:48 +08:00
|
|
|
llvm::StringMap<unsigned>::iterator Pos
|
2010-08-17 05:23:13 +08:00
|
|
|
= CachedCompletionTypes.find(QualType(Expected).getAsString());
|
2010-08-17 04:01:48 +08:00
|
|
|
if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
|
|
|
|
Priority /= CCF_ExactTypeMatch;
|
|
|
|
else
|
|
|
|
Priority /= CCF_SimilarTypeMatch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-25 04:21:13 +08:00
|
|
|
// Adjust the completion string, if required.
|
|
|
|
if (C->Kind == CXCursor_MacroDefinition &&
|
|
|
|
Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
|
|
|
|
// Create a new code-completion string that just contains the
|
|
|
|
// macro name, without its arguments.
|
2012-04-11 01:23:48 +08:00
|
|
|
CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
|
|
|
|
CCP_CodePattern, C->Availability);
|
2011-02-02 03:23:04 +08:00
|
|
|
Builder.AddTypedTextChunk(C->Completion->getTypedText());
|
2010-08-26 02:03:13 +08:00
|
|
|
Priority = CCP_CodePattern;
|
2011-02-02 03:23:04 +08:00
|
|
|
Completion = Builder.TakeString();
|
2010-08-25 04:21:13 +08:00
|
|
|
}
|
|
|
|
|
2012-09-27 08:24:09 +08:00
|
|
|
AllResults.push_back(Result(Completion, Priority, C->Kind,
|
2010-08-24 07:00:57 +08:00
|
|
|
C->Availability));
|
2010-08-17 04:01:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we did not add any cached completion results, just forward the
|
|
|
|
// results we were given to the next consumer.
|
|
|
|
if (!AddedResult) {
|
|
|
|
Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
|
|
|
|
return;
|
|
|
|
}
|
2010-08-26 21:48:20 +08:00
|
|
|
|
2010-08-17 04:01:48 +08:00
|
|
|
Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
|
|
|
|
AllResults.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
|
2014-02-07 23:00:22 +08:00
|
|
|
ArrayRef<RemappedFile> RemappedFiles,
|
2010-08-05 17:09:23 +08:00
|
|
|
bool IncludeMacros,
|
|
|
|
bool IncludeCodePatterns,
|
2012-07-03 01:35:10 +08:00
|
|
|
bool IncludeBriefComments,
|
2010-08-05 00:47:14 +08:00
|
|
|
CodeCompleteConsumer &Consumer,
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diag, LangOptions &LangOpts,
|
2010-08-05 00:47:14 +08:00
|
|
|
SourceManager &SourceMgr, FileManager &FileMgr,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
|
|
|
|
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
|
2011-03-22 02:40:17 +08:00
|
|
|
if (!Invocation)
|
2010-08-05 00:47:14 +08:00
|
|
|
return;
|
|
|
|
|
2010-10-28 23:44:59 +08:00
|
|
|
SimpleTimer CompletionTimer(WantTiming);
|
2010-11-10 04:00:56 +08:00
|
|
|
CompletionTimer.setOutput("Code completion @ " + File + ":" +
|
2011-07-23 18:55:15 +08:00
|
|
|
Twine(Line) + ":" + Twine(Column));
|
2010-08-10 04:45:32 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<CompilerInvocation>
|
2011-03-22 02:40:17 +08:00
|
|
|
CCInvocation(new CompilerInvocation(*Invocation));
|
|
|
|
|
|
|
|
FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
|
2012-07-03 01:35:10 +08:00
|
|
|
CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
|
2011-03-22 02:40:17 +08:00
|
|
|
PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
|
2010-08-05 17:09:23 +08:00
|
|
|
|
2012-07-03 01:35:10 +08:00
|
|
|
CodeCompleteOpts.IncludeMacros = IncludeMacros &&
|
|
|
|
CachedCompletionResults.empty();
|
|
|
|
CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
|
|
|
|
CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
|
|
|
|
CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
|
|
|
|
|
|
|
|
assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
|
|
|
|
|
2010-08-05 00:47:14 +08:00
|
|
|
FrontendOpts.CodeCompletionAt.FileName = File;
|
|
|
|
FrontendOpts.CodeCompletionAt.Line = Line;
|
|
|
|
FrontendOpts.CodeCompletionAt.Column = Column;
|
|
|
|
|
|
|
|
// Set the language options appropriately.
|
2011-11-18 07:01:24 +08:00
|
|
|
LangOpts = *CCInvocation->getLangOpts();
|
2010-08-05 00:47:14 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<CompilerInstance> Clang(new CompilerInstance());
|
2011-03-22 02:40:07 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2011-03-22 09:15:24 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
|
|
|
|
CICleanup(Clang.get());
|
2011-03-22 02:40:07 +08:00
|
|
|
|
2011-03-22 02:40:17 +08:00
|
|
|
Clang->setInvocation(&*CCInvocation);
|
2012-11-10 03:40:39 +08:00
|
|
|
OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
|
2010-08-05 00:47:14 +08:00
|
|
|
|
|
|
|
// Set up diagnostics, capturing any diagnostics produced.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setDiagnostics(&Diag);
|
2010-08-05 00:47:14 +08:00
|
|
|
CaptureDroppedDiagnostics Capture(true,
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->getDiagnostics(),
|
2010-08-05 00:47:14 +08:00
|
|
|
StoredDiagnostics);
|
2013-07-18 22:23:12 +08:00
|
|
|
ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
|
2010-08-05 00:47:14 +08:00
|
|
|
|
|
|
|
// Create the target instance.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
|
2012-11-16 12:24:59 +08:00
|
|
|
&Clang->getTargetOpts()));
|
2011-03-22 02:40:07 +08:00
|
|
|
if (!Clang->hasTarget()) {
|
2011-03-22 02:40:17 +08:00
|
|
|
Clang->setInvocation(0);
|
2010-08-19 06:29:43 +08:00
|
|
|
return;
|
2010-08-05 00:47:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inform the target of the language options.
|
|
|
|
//
|
|
|
|
// FIXME: We shouldn't need to do this, the target should be immutable once
|
|
|
|
// created. This complexity should be lifted elsewhere.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
|
2010-08-05 00:47:14 +08:00
|
|
|
|
2011-03-22 02:40:07 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
|
2010-08-05 00:47:14 +08:00
|
|
|
"Invocation must have exactly one source file!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
|
2010-08-05 00:47:14 +08:00
|
|
|
"FIXME: AST inputs not yet supported here!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
|
2010-08-05 00:47:14 +08:00
|
|
|
"IR inputs not support here!");
|
|
|
|
|
|
|
|
|
|
|
|
// Use the source and file managers that we were given.
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setFileManager(&FileMgr);
|
|
|
|
Clang->setSourceManager(&SourceMgr);
|
2010-08-05 00:47:14 +08:00
|
|
|
|
|
|
|
// Remap files.
|
|
|
|
PreprocessorOpts.clearRemappedFiles();
|
2010-08-05 01:07:00 +08:00
|
|
|
PreprocessorOpts.RetainRemappedFileBuffers = true;
|
2014-02-07 23:00:22 +08:00
|
|
|
for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) {
|
2014-02-08 08:38:15 +08:00
|
|
|
PreprocessorOpts.addRemappedFile(RemappedFiles[I].first,
|
|
|
|
RemappedFiles[I].second);
|
2010-08-20 08:59:43 +08:00
|
|
|
}
|
2014-02-08 08:38:15 +08:00
|
|
|
|
2010-08-14 06:48:40 +08:00
|
|
|
// Use the code completion consumer we were given, but adding any cached
|
|
|
|
// code-completion results.
|
2010-11-30 00:13:56 +08:00
|
|
|
AugmentedCodeCompleteConsumer *AugmentedConsumer
|
2012-07-03 01:35:10 +08:00
|
|
|
= new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
|
2011-03-22 02:40:07 +08:00
|
|
|
Clang->setCodeCompletionConsumer(AugmentedConsumer);
|
2010-08-05 00:47:14 +08:00
|
|
|
|
2010-08-10 04:45:32 +08:00
|
|
|
// If we have a precompiled preamble, try to use it. We only allow
|
|
|
|
// the use of the precompiled preamble if we're if the completion
|
|
|
|
// point is within the main file, after the end of the precompiled
|
|
|
|
// preamble.
|
|
|
|
llvm::MemoryBuffer *OverrideMainBuffer = 0;
|
2011-10-28 01:55:18 +08:00
|
|
|
if (!getPreambleFile(this).empty()) {
|
2013-06-19 03:40:07 +08:00
|
|
|
std::string CompleteFilePath(File);
|
2013-07-30 05:26:52 +08:00
|
|
|
llvm::sys::fs::UniqueID CompleteFileID;
|
2013-06-19 03:40:07 +08:00
|
|
|
|
2013-06-20 23:12:38 +08:00
|
|
|
if (!llvm::sys::fs::getUniqueID(CompleteFilePath, CompleteFileID)) {
|
2013-06-19 03:40:07 +08:00
|
|
|
std::string MainPath(OriginalSourceFile);
|
2013-07-30 05:26:52 +08:00
|
|
|
llvm::sys::fs::UniqueID MainID;
|
2013-06-20 23:12:38 +08:00
|
|
|
if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) {
|
2013-06-19 03:40:07 +08:00
|
|
|
if (CompleteFileID == MainID && Line > 1)
|
2010-08-20 08:59:43 +08:00
|
|
|
OverrideMainBuffer
|
2011-03-22 02:40:17 +08:00
|
|
|
= getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
|
2010-08-26 02:04:15 +08:00
|
|
|
Line - 1);
|
2013-06-19 03:40:07 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-10 04:45:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the main file has been overridden due to the use of a preamble,
|
|
|
|
// make that override happen and introduce the preamble.
|
|
|
|
if (OverrideMainBuffer) {
|
|
|
|
PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
|
|
|
|
PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
|
|
|
|
PreprocessorOpts.PrecompiledPreambleBytes.second
|
|
|
|
= PreambleEndsAtStartOfLine;
|
2011-10-28 01:55:18 +08:00
|
|
|
PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
|
2010-08-10 04:45:32 +08:00
|
|
|
PreprocessorOpts.DisablePCHValidation = true;
|
|
|
|
|
2010-08-20 08:59:43 +08:00
|
|
|
OwnedBuffers.push_back(OverrideMainBuffer);
|
2010-08-20 08:02:33 +08:00
|
|
|
} else {
|
|
|
|
PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
|
|
|
|
PreprocessorOpts.PrecompiledPreambleBytes.second = false;
|
2010-08-10 04:45:32 +08:00
|
|
|
}
|
|
|
|
|
2012-11-03 06:18:44 +08:00
|
|
|
// Disable the preprocessing record if modules are not enabled.
|
|
|
|
if (!Clang->getLangOpts().Modules)
|
|
|
|
PreprocessorOpts.DetailedRecord = false;
|
2011-05-07 00:33:08 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<SyntaxOnlyAction> Act;
|
2010-08-05 00:47:14 +08:00
|
|
|
Act.reset(new SyntaxOnlyAction);
|
2012-01-21 00:28:04 +08:00
|
|
|
if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
|
2010-08-05 00:47:14 +08:00
|
|
|
Act->Execute();
|
|
|
|
Act->EndSourceFile();
|
|
|
|
}
|
|
|
|
}
|
2010-08-13 13:36:37 +08:00
|
|
|
|
2012-09-27 00:39:46 +08:00
|
|
|
bool ASTUnit::Save(StringRef File) {
|
2013-05-24 13:44:08 +08:00
|
|
|
if (HadModuleLoaderFatalFailure)
|
|
|
|
return true;
|
|
|
|
|
2011-07-22 02:44:49 +08:00
|
|
|
// Write to a temporary file and later rename it to the actual file, to avoid
|
|
|
|
// possible race conditions.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> TempPath;
|
2011-07-28 08:45:10 +08:00
|
|
|
TempPath = File;
|
|
|
|
TempPath += "-%%%%%%%%";
|
|
|
|
int fd;
|
2013-07-06 05:13:58 +08:00
|
|
|
if (llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath))
|
2012-09-27 00:39:46 +08:00
|
|
|
return true;
|
2011-07-22 02:44:49 +08:00
|
|
|
|
2010-08-13 13:36:37 +08:00
|
|
|
// FIXME: Can we somehow regenerate the stat cache here, or do we need to
|
|
|
|
// unconditionally create a stat cache when we parse the file?
|
2011-07-28 08:45:10 +08:00
|
|
|
llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
|
2011-03-10 01:21:42 +08:00
|
|
|
|
|
|
|
serialize(Out);
|
|
|
|
Out.close();
|
2012-03-13 10:17:06 +08:00
|
|
|
if (Out.has_error()) {
|
|
|
|
Out.clear_error();
|
2012-09-27 00:39:46 +08:00
|
|
|
return true;
|
2012-03-13 10:17:06 +08:00
|
|
|
}
|
2011-07-22 02:44:49 +08:00
|
|
|
|
2011-12-25 09:18:52 +08:00
|
|
|
if (llvm::sys::fs::rename(TempPath.str(), File)) {
|
2014-01-11 05:32:14 +08:00
|
|
|
llvm::sys::fs::remove(TempPath.str());
|
2012-09-27 00:39:46 +08:00
|
|
|
return true;
|
2011-07-22 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
2012-09-27 00:39:46 +08:00
|
|
|
return false;
|
2011-03-10 01:21:42 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 00:05:00 +08:00
|
|
|
static bool serializeUnit(ASTWriter &Writer,
|
|
|
|
SmallVectorImpl<char> &Buffer,
|
|
|
|
Sema &S,
|
|
|
|
bool hasErrors,
|
|
|
|
raw_ostream &OS) {
|
2012-11-01 04:59:50 +08:00
|
|
|
Writer.WriteAST(S, std::string(), 0, "", hasErrors);
|
2012-10-12 00:05:00 +08:00
|
|
|
|
|
|
|
// Write the generated bitstream to "Out".
|
|
|
|
if (!Buffer.empty())
|
|
|
|
OS.write(Buffer.data(), Buffer.size());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
bool ASTUnit::serialize(raw_ostream &OS) {
|
2012-03-07 09:51:17 +08:00
|
|
|
bool hasErrors = getDiagnostics().hasErrorOccurred();
|
2011-03-10 01:21:42 +08:00
|
|
|
|
2012-10-12 00:05:00 +08:00
|
|
|
if (WriterData)
|
|
|
|
return serializeUnit(WriterData->Writer, WriterData->Buffer,
|
|
|
|
getSema(), hasErrors, OS);
|
|
|
|
|
2012-03-01 04:31:23 +08:00
|
|
|
SmallString<128> Buffer;
|
2010-08-13 13:36:37 +08:00
|
|
|
llvm::BitstreamWriter Stream(Buffer);
|
2010-08-19 07:56:21 +08:00
|
|
|
ASTWriter Writer(Stream);
|
2012-10-12 00:05:00 +08:00
|
|
|
return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
|
2010-08-13 13:36:37 +08:00
|
|
|
}
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
|
|
|
|
typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
|
|
|
|
|
|
|
|
static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) {
|
|
|
|
unsigned Raw = L.getRawEncoding();
|
|
|
|
const unsigned MacroBit = 1U << 31;
|
|
|
|
L = SourceLocation::getFromRawEncoding((Raw & MacroBit) |
|
|
|
|
((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTUnit::TranslateStoredDiagnostics(
|
|
|
|
ASTReader *MMan,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef ModName,
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
SourceManager &SrcMgr,
|
2011-07-23 18:55:15 +08:00
|
|
|
const SmallVectorImpl<StoredDiagnostic> &Diags,
|
|
|
|
SmallVectorImpl<StoredDiagnostic> &Out) {
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
// The stored diagnostic has the old source manager in it; update
|
|
|
|
// the locations to refer into the new source manager. We also need to remap
|
|
|
|
// all the locations to the new view. This includes the diag location, any
|
|
|
|
// associated source ranges, and the source ranges of associated fix-its.
|
|
|
|
// FIXME: There should be a cleaner way to do this.
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<StoredDiagnostic, 4> Result;
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
Result.reserve(Diags.size());
|
|
|
|
assert(MMan && "Don't have a module manager");
|
2011-12-01 07:21:26 +08:00
|
|
|
serialization::ModuleFile *Mod = MMan->ModuleMgr.lookup(ModName);
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
assert(Mod && "Don't have preamble module");
|
|
|
|
SLocRemap &Remap = Mod->SLocRemap;
|
|
|
|
for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
|
|
|
|
// Rebuild the StoredDiagnostic.
|
|
|
|
const StoredDiagnostic &SD = Diags[I];
|
|
|
|
SourceLocation L = SD.getLocation();
|
|
|
|
TranslateSLoc(L, Remap);
|
|
|
|
FullSourceLoc Loc(L, SrcMgr);
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<CharSourceRange, 4> Ranges;
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
Ranges.reserve(SD.range_size());
|
|
|
|
for (StoredDiagnostic::range_iterator I = SD.range_begin(),
|
|
|
|
E = SD.range_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
SourceLocation BL = I->getBegin();
|
|
|
|
TranslateSLoc(BL, Remap);
|
|
|
|
SourceLocation EL = I->getEnd();
|
|
|
|
TranslateSLoc(EL, Remap);
|
|
|
|
Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange()));
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<FixItHint, 2> FixIts;
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
FixIts.reserve(SD.fixit_size());
|
|
|
|
for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(),
|
|
|
|
E = SD.fixit_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
FixIts.push_back(FixItHint());
|
|
|
|
FixItHint &FH = FixIts.back();
|
|
|
|
FH.CodeToInsert = I->CodeToInsert;
|
|
|
|
SourceLocation BL = I->RemoveRange.getBegin();
|
|
|
|
TranslateSLoc(BL, Remap);
|
|
|
|
SourceLocation EL = I->RemoveRange.getEnd();
|
|
|
|
TranslateSLoc(EL, Remap);
|
|
|
|
FH.RemoveRange = CharSourceRange(SourceRange(BL, EL),
|
|
|
|
I->RemoveRange.isTokenRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(),
|
|
|
|
SD.getMessage(), Loc, Ranges, FixIts));
|
|
|
|
}
|
|
|
|
Result.swap(Out);
|
|
|
|
}
|
2011-09-20 04:40:35 +08:00
|
|
|
|
2011-10-31 15:19:59 +08:00
|
|
|
void ASTUnit::addFileLevelDecl(Decl *D) {
|
|
|
|
assert(D);
|
2011-11-08 02:53:57 +08:00
|
|
|
|
|
|
|
// We only care about local declarations.
|
|
|
|
if (D->isFromASTFile())
|
|
|
|
return;
|
2011-10-31 15:19:59 +08:00
|
|
|
|
|
|
|
SourceManager &SM = *SourceMgr;
|
|
|
|
SourceLocation Loc = D->getLocation();
|
|
|
|
if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We only keep track of the file-level declarations of each file.
|
|
|
|
if (!D->getLexicalDeclContext()->isFileContext())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SourceLocation FileLoc = SM.getFileLoc(Loc);
|
|
|
|
assert(SM.isLocalSourceLocation(FileLoc));
|
|
|
|
FileID FID;
|
|
|
|
unsigned Offset;
|
|
|
|
llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
LocDeclsTy *&Decls = FileDecls[FID];
|
|
|
|
if (!Decls)
|
|
|
|
Decls = new LocDeclsTy();
|
|
|
|
|
|
|
|
std::pair<unsigned, Decl *> LocDecl(Offset, D);
|
|
|
|
|
|
|
|
if (Decls->empty() || Decls->back().first <= Offset) {
|
|
|
|
Decls->push_back(LocDecl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-24 21:22:59 +08:00
|
|
|
LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
|
|
|
|
LocDecl, llvm::less_first());
|
2011-10-31 15:19:59 +08:00
|
|
|
|
|
|
|
Decls->insert(I, LocDecl);
|
|
|
|
}
|
|
|
|
|
2011-11-03 10:20:32 +08:00
|
|
|
void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
|
|
|
|
SmallVectorImpl<Decl *> &Decls) {
|
|
|
|
if (File.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (SourceMgr->isLoadedFileID(File)) {
|
|
|
|
assert(Ctx->getExternalSource() && "No external source!");
|
|
|
|
return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
|
|
|
|
Decls);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileDeclsTy::iterator I = FileDecls.find(File);
|
|
|
|
if (I == FileDecls.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
LocDeclsTy &LocDecls = *I->second;
|
|
|
|
if (LocDecls.empty())
|
|
|
|
return;
|
|
|
|
|
2013-08-24 21:12:34 +08:00
|
|
|
LocDeclsTy::iterator BeginIt =
|
|
|
|
std::lower_bound(LocDecls.begin(), LocDecls.end(),
|
2013-08-24 21:22:59 +08:00
|
|
|
std::make_pair(Offset, (Decl *)0), llvm::less_first());
|
2011-11-03 10:20:32 +08:00
|
|
|
if (BeginIt != LocDecls.begin())
|
|
|
|
--BeginIt;
|
|
|
|
|
2011-11-24 04:27:36 +08:00
|
|
|
// If we are pointing at a top-level decl inside an objc container, we need
|
|
|
|
// to backtrack until we find it otherwise we will fail to report that the
|
|
|
|
// region overlaps with an objc container.
|
|
|
|
while (BeginIt != LocDecls.begin() &&
|
|
|
|
BeginIt->second->isTopLevelDeclInObjCContainer())
|
|
|
|
--BeginIt;
|
|
|
|
|
2013-08-24 21:12:34 +08:00
|
|
|
LocDeclsTy::iterator EndIt = std::upper_bound(
|
|
|
|
LocDecls.begin(), LocDecls.end(),
|
2013-08-24 21:22:59 +08:00
|
|
|
std::make_pair(Offset + Length, (Decl *)0), llvm::less_first());
|
2011-11-03 10:20:32 +08:00
|
|
|
if (EndIt != LocDecls.end())
|
|
|
|
++EndIt;
|
|
|
|
|
|
|
|
for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
|
|
|
|
Decls.push_back(DIt->second);
|
|
|
|
}
|
|
|
|
|
2011-09-20 04:40:35 +08:00
|
|
|
SourceLocation ASTUnit::getLocation(const FileEntry *File,
|
|
|
|
unsigned Line, unsigned Col) const {
|
|
|
|
const SourceManager &SM = getSourceManager();
|
2011-09-26 16:01:41 +08:00
|
|
|
SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
|
2011-09-20 04:40:35 +08:00
|
|
|
return SM.getMacroArgExpandedLocation(Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation ASTUnit::getLocation(const FileEntry *File,
|
|
|
|
unsigned Offset) const {
|
|
|
|
const SourceManager &SM = getSourceManager();
|
2011-09-26 16:01:41 +08:00
|
|
|
SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
|
2011-09-20 04:40:35 +08:00
|
|
|
return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
|
|
|
|
}
|
|
|
|
|
2011-09-26 16:01:41 +08:00
|
|
|
/// \brief If \arg Loc is a loaded location from the preamble, returns
|
|
|
|
/// the corresponding local location of the main file, otherwise it returns
|
|
|
|
/// \arg Loc.
|
|
|
|
SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
|
|
|
|
FileID PreambleID;
|
|
|
|
if (SourceMgr)
|
|
|
|
PreambleID = SourceMgr->getPreambleFileID();
|
|
|
|
|
|
|
|
if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
|
|
|
|
return Loc;
|
|
|
|
|
|
|
|
unsigned Offs;
|
|
|
|
if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
|
|
|
|
SourceLocation FileLoc
|
|
|
|
= SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
|
|
|
|
return FileLoc.getLocWithOffset(Offs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief If \arg Loc is a local location of the main file but inside the
|
|
|
|
/// preamble chunk, returns the corresponding loaded location from the
|
|
|
|
/// preamble, otherwise it returns \arg Loc.
|
|
|
|
SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
|
|
|
|
FileID PreambleID;
|
|
|
|
if (SourceMgr)
|
|
|
|
PreambleID = SourceMgr->getPreambleFileID();
|
|
|
|
|
|
|
|
if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
|
|
|
|
return Loc;
|
|
|
|
|
|
|
|
unsigned Offs;
|
|
|
|
if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
|
|
|
|
Offs < Preamble.size()) {
|
|
|
|
SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
|
|
|
|
return FileLoc.getLocWithOffset(Offs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
2011-10-25 08:29:50 +08:00
|
|
|
bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
|
|
|
|
FileID FID;
|
|
|
|
if (SourceMgr)
|
|
|
|
FID = SourceMgr->getPreambleFileID();
|
|
|
|
|
|
|
|
if (Loc.isInvalid() || FID.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return SourceMgr->isInFileID(Loc, FID);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTUnit::isInMainFileID(SourceLocation Loc) {
|
|
|
|
FileID FID;
|
|
|
|
if (SourceMgr)
|
|
|
|
FID = SourceMgr->getMainFileID();
|
|
|
|
|
|
|
|
if (Loc.isInvalid() || FID.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return SourceMgr->isInFileID(Loc, FID);
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation ASTUnit::getEndOfPreambleFileID() {
|
|
|
|
FileID FID;
|
|
|
|
if (SourceMgr)
|
|
|
|
FID = SourceMgr->getPreambleFileID();
|
|
|
|
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return SourceLocation();
|
|
|
|
|
|
|
|
return SourceMgr->getLocForEndOfFile(FID);
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation ASTUnit::getStartOfMainFileID() {
|
|
|
|
FileID FID;
|
|
|
|
if (SourceMgr)
|
|
|
|
FID = SourceMgr->getMainFileID();
|
|
|
|
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return SourceLocation();
|
|
|
|
|
|
|
|
return SourceMgr->getLocForStartOfFile(FID);
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:10:51 +08:00
|
|
|
std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
|
|
|
|
ASTUnit::getLocalPreprocessingEntities() const {
|
|
|
|
if (isMainFileAST()) {
|
|
|
|
serialization::ModuleFile &
|
|
|
|
Mod = Reader->getModuleManager().getPrimaryModule();
|
|
|
|
return Reader->getModulePreprocessedEntities(Mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
|
|
|
|
return std::make_pair(PPRec->local_begin(), PPRec->local_end());
|
|
|
|
|
|
|
|
return std::make_pair(PreprocessingRecord::iterator(),
|
|
|
|
PreprocessingRecord::iterator());
|
|
|
|
}
|
|
|
|
|
2012-10-03 09:58:28 +08:00
|
|
|
bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
|
2012-10-03 05:09:13 +08:00
|
|
|
if (isMainFileAST()) {
|
|
|
|
serialization::ModuleFile &
|
|
|
|
Mod = Reader->getModuleManager().getPrimaryModule();
|
|
|
|
ASTReader::ModuleDeclIterator MDI, MDE;
|
|
|
|
llvm::tie(MDI, MDE) = Reader->getModuleFileLevelDecls(Mod);
|
|
|
|
for (; MDI != MDE; ++MDI) {
|
|
|
|
if (!Fn(context, *MDI))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ASTUnit::top_level_iterator TL = top_level_begin(),
|
|
|
|
TLEnd = top_level_end();
|
|
|
|
TL != TLEnd; ++TL) {
|
|
|
|
if (!Fn(context, *TL))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-04 05:05:51 +08:00
|
|
|
namespace {
|
|
|
|
struct PCHLocatorInfo {
|
|
|
|
serialization::ModuleFile *Mod;
|
|
|
|
PCHLocatorInfo() : Mod(0) {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PCHLocator(serialization::ModuleFile &M, void *UserData) {
|
|
|
|
PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData);
|
|
|
|
switch (M.Kind) {
|
|
|
|
case serialization::MK_Module:
|
|
|
|
return true; // skip dependencies.
|
|
|
|
case serialization::MK_PCH:
|
|
|
|
Info.Mod = &M;
|
|
|
|
return true; // found it.
|
|
|
|
case serialization::MK_Preamble:
|
|
|
|
return false; // look in dependencies.
|
|
|
|
case serialization::MK_MainFile:
|
|
|
|
return false; // look in dependencies.
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FileEntry *ASTUnit::getPCHFile() {
|
|
|
|
if (!Reader)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
PCHLocatorInfo Info;
|
|
|
|
Reader->getModuleManager().visit(PCHLocator, &Info);
|
|
|
|
if (Info.Mod)
|
|
|
|
return Info.Mod->File;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-10 10:12:47 +08:00
|
|
|
bool ASTUnit::isModuleFile() {
|
|
|
|
return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
|
|
|
|
}
|
|
|
|
|
2011-09-20 04:40:35 +08:00
|
|
|
void ASTUnit::PreambleData::countLines() const {
|
|
|
|
NumLines = 0;
|
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (std::vector<char>::const_iterator
|
|
|
|
I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
|
|
|
|
if (*I == '\n')
|
|
|
|
++NumLines;
|
|
|
|
}
|
|
|
|
if (Buffer.back() != '\n')
|
|
|
|
++NumLines;
|
|
|
|
}
|
2011-10-11 05:57:12 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
ASTUnit::ConcurrencyState::ConcurrencyState() {
|
|
|
|
Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTUnit::ConcurrencyState::~ConcurrencyState() {
|
|
|
|
delete static_cast<llvm::sys::MutexImpl *>(Mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTUnit::ConcurrencyState::start() {
|
|
|
|
bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
|
|
|
|
assert(acquired && "Concurrent access to ASTUnit!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTUnit::ConcurrencyState::finish() {
|
|
|
|
static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // NDEBUG
|
|
|
|
|
2013-11-22 15:49:39 +08:00
|
|
|
ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = 0; }
|
2011-10-11 05:57:12 +08:00
|
|
|
ASTUnit::ConcurrencyState::~ConcurrencyState() {}
|
|
|
|
void ASTUnit::ConcurrencyState::start() {}
|
|
|
|
void ASTUnit::ConcurrencyState::finish() {}
|
|
|
|
|
|
|
|
#endif
|