2010-01-06 03:32:54 +08:00
|
|
|
//===- CIndexCodeCompletion.cpp - Code Completion API hooks ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Clang-C Source Indexing library hooks for
|
|
|
|
// code completion.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CIndexer.h"
|
2010-11-18 07:24:11 +08:00
|
|
|
#include "CXTranslationUnit.h"
|
2010-11-16 09:56:27 +08:00
|
|
|
#include "CXString.h"
|
2010-01-28 08:56:43 +08:00
|
|
|
#include "CIndexDiagnostic.h"
|
2010-04-13 03:45:50 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2010-08-05 00:47:14 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2010-04-13 03:45:50 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2010-01-28 08:56:43 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2010-01-06 03:32:54 +08:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2010-08-10 04:45:32 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-01-06 03:32:54 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2010-12-07 08:05:48 +08:00
|
|
|
#include "llvm/Support/Atomic.h"
|
2010-08-20 07:44:10 +08:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2010-01-06 03:32:54 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-08-10 04:45:32 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2010-07-27 00:29:14 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
2010-01-06 03:32:54 +08:00
|
|
|
|
2010-08-10 04:45:32 +08:00
|
|
|
|
2010-04-15 09:02:28 +08:00
|
|
|
#ifdef UDP_CODE_COMPLETION_LOGGER
|
|
|
|
#include "clang/Basic/Version.h"
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
using namespace clang;
|
2010-02-17 09:42:24 +08:00
|
|
|
using namespace clang::cxstring;
|
2010-01-06 03:32:54 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
enum CXCompletionChunkKind
|
|
|
|
clang_getCompletionChunkKind(CXCompletionString completion_string,
|
|
|
|
unsigned chunk_number) {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
|
2010-01-06 03:32:54 +08:00
|
|
|
if (!CCStr || chunk_number >= CCStr->size())
|
|
|
|
return CXCompletionChunk_Text;
|
|
|
|
|
|
|
|
switch ((*CCStr)[chunk_number].Kind) {
|
|
|
|
case CodeCompletionString::CK_TypedText:
|
|
|
|
return CXCompletionChunk_TypedText;
|
|
|
|
case CodeCompletionString::CK_Text:
|
|
|
|
return CXCompletionChunk_Text;
|
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
return CXCompletionChunk_Optional;
|
|
|
|
case CodeCompletionString::CK_Placeholder:
|
|
|
|
return CXCompletionChunk_Placeholder;
|
|
|
|
case CodeCompletionString::CK_Informative:
|
|
|
|
return CXCompletionChunk_Informative;
|
|
|
|
case CodeCompletionString::CK_ResultType:
|
|
|
|
return CXCompletionChunk_ResultType;
|
|
|
|
case CodeCompletionString::CK_CurrentParameter:
|
|
|
|
return CXCompletionChunk_CurrentParameter;
|
|
|
|
case CodeCompletionString::CK_LeftParen:
|
|
|
|
return CXCompletionChunk_LeftParen;
|
|
|
|
case CodeCompletionString::CK_RightParen:
|
|
|
|
return CXCompletionChunk_RightParen;
|
|
|
|
case CodeCompletionString::CK_LeftBracket:
|
|
|
|
return CXCompletionChunk_LeftBracket;
|
|
|
|
case CodeCompletionString::CK_RightBracket:
|
|
|
|
return CXCompletionChunk_RightBracket;
|
|
|
|
case CodeCompletionString::CK_LeftBrace:
|
|
|
|
return CXCompletionChunk_LeftBrace;
|
|
|
|
case CodeCompletionString::CK_RightBrace:
|
|
|
|
return CXCompletionChunk_RightBrace;
|
|
|
|
case CodeCompletionString::CK_LeftAngle:
|
|
|
|
return CXCompletionChunk_LeftAngle;
|
|
|
|
case CodeCompletionString::CK_RightAngle:
|
|
|
|
return CXCompletionChunk_RightAngle;
|
|
|
|
case CodeCompletionString::CK_Comma:
|
|
|
|
return CXCompletionChunk_Comma;
|
Improve code completion by introducing patterns for the various C and
C++ grammatical constructs that show up in top-level (namespace-level)
declarations, member declarations, template declarations, statements,
expressions, conditions, etc. For example, we now provide a pattern
for
static_cast<type>(expr)
when we can have an expression, or
using namespace identifier;
when we can have a using directive.
Also, improves the results of code completion at the beginning of a
top-level declaration. Previously, we would see value names (function
names, global variables, etc.); now we see types, namespace names,
etc., but no values.
llvm-svn: 93134
2010-01-11 07:08:15 +08:00
|
|
|
case CodeCompletionString::CK_Colon:
|
|
|
|
return CXCompletionChunk_Colon;
|
|
|
|
case CodeCompletionString::CK_SemiColon:
|
|
|
|
return CXCompletionChunk_SemiColon;
|
|
|
|
case CodeCompletionString::CK_Equal:
|
|
|
|
return CXCompletionChunk_Equal;
|
|
|
|
case CodeCompletionString::CK_HorizontalSpace:
|
|
|
|
return CXCompletionChunk_HorizontalSpace;
|
|
|
|
case CodeCompletionString::CK_VerticalSpace:
|
|
|
|
return CXCompletionChunk_VerticalSpace;
|
2010-01-06 03:32:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be unreachable, but let's be careful.
|
|
|
|
return CXCompletionChunk_Text;
|
|
|
|
}
|
|
|
|
|
2010-02-17 09:42:24 +08:00
|
|
|
CXString clang_getCompletionChunkText(CXCompletionString completion_string,
|
|
|
|
unsigned chunk_number) {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
|
2010-01-06 03:32:54 +08:00
|
|
|
if (!CCStr || chunk_number >= CCStr->size())
|
2010-11-16 16:15:36 +08:00
|
|
|
return createCXString((const char*)0);
|
2010-01-06 03:32:54 +08:00
|
|
|
|
|
|
|
switch ((*CCStr)[chunk_number].Kind) {
|
|
|
|
case CodeCompletionString::CK_TypedText:
|
|
|
|
case CodeCompletionString::CK_Text:
|
|
|
|
case CodeCompletionString::CK_Placeholder:
|
|
|
|
case CodeCompletionString::CK_CurrentParameter:
|
|
|
|
case CodeCompletionString::CK_Informative:
|
|
|
|
case CodeCompletionString::CK_LeftParen:
|
|
|
|
case CodeCompletionString::CK_RightParen:
|
|
|
|
case CodeCompletionString::CK_LeftBracket:
|
|
|
|
case CodeCompletionString::CK_RightBracket:
|
|
|
|
case CodeCompletionString::CK_LeftBrace:
|
|
|
|
case CodeCompletionString::CK_RightBrace:
|
|
|
|
case CodeCompletionString::CK_LeftAngle:
|
|
|
|
case CodeCompletionString::CK_RightAngle:
|
|
|
|
case CodeCompletionString::CK_Comma:
|
|
|
|
case CodeCompletionString::CK_ResultType:
|
Improve code completion by introducing patterns for the various C and
C++ grammatical constructs that show up in top-level (namespace-level)
declarations, member declarations, template declarations, statements,
expressions, conditions, etc. For example, we now provide a pattern
for
static_cast<type>(expr)
when we can have an expression, or
using namespace identifier;
when we can have a using directive.
Also, improves the results of code completion at the beginning of a
top-level declaration. Previously, we would see value names (function
names, global variables, etc.); now we see types, namespace names,
etc., but no values.
llvm-svn: 93134
2010-01-11 07:08:15 +08:00
|
|
|
case CodeCompletionString::CK_Colon:
|
|
|
|
case CodeCompletionString::CK_SemiColon:
|
|
|
|
case CodeCompletionString::CK_Equal:
|
|
|
|
case CodeCompletionString::CK_HorizontalSpace:
|
2010-05-25 14:14:46 +08:00
|
|
|
case CodeCompletionString::CK_VerticalSpace:
|
2010-10-09 04:39:29 +08:00
|
|
|
return createCXString((*CCStr)[chunk_number].Text, false);
|
2010-05-25 14:14:46 +08:00
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
// Note: treated as an empty text block.
|
2010-02-17 09:42:24 +08:00
|
|
|
return createCXString("");
|
2010-01-06 03:32:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be unreachable, but let's be careful.
|
2010-11-16 16:15:36 +08:00
|
|
|
return createCXString((const char*)0);
|
2010-01-06 03:32:54 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 09:42:24 +08:00
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
CXCompletionString
|
|
|
|
clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
|
|
|
|
unsigned chunk_number) {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
|
2010-01-06 03:32:54 +08:00
|
|
|
if (!CCStr || chunk_number >= CCStr->size())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch ((*CCStr)[chunk_number].Kind) {
|
|
|
|
case CodeCompletionString::CK_TypedText:
|
|
|
|
case CodeCompletionString::CK_Text:
|
|
|
|
case CodeCompletionString::CK_Placeholder:
|
|
|
|
case CodeCompletionString::CK_CurrentParameter:
|
|
|
|
case CodeCompletionString::CK_Informative:
|
|
|
|
case CodeCompletionString::CK_LeftParen:
|
|
|
|
case CodeCompletionString::CK_RightParen:
|
|
|
|
case CodeCompletionString::CK_LeftBracket:
|
|
|
|
case CodeCompletionString::CK_RightBracket:
|
|
|
|
case CodeCompletionString::CK_LeftBrace:
|
|
|
|
case CodeCompletionString::CK_RightBrace:
|
|
|
|
case CodeCompletionString::CK_LeftAngle:
|
|
|
|
case CodeCompletionString::CK_RightAngle:
|
|
|
|
case CodeCompletionString::CK_Comma:
|
|
|
|
case CodeCompletionString::CK_ResultType:
|
Improve code completion by introducing patterns for the various C and
C++ grammatical constructs that show up in top-level (namespace-level)
declarations, member declarations, template declarations, statements,
expressions, conditions, etc. For example, we now provide a pattern
for
static_cast<type>(expr)
when we can have an expression, or
using namespace identifier;
when we can have a using directive.
Also, improves the results of code completion at the beginning of a
top-level declaration. Previously, we would see value names (function
names, global variables, etc.); now we see types, namespace names,
etc., but no values.
llvm-svn: 93134
2010-01-11 07:08:15 +08:00
|
|
|
case CodeCompletionString::CK_Colon:
|
|
|
|
case CodeCompletionString::CK_SemiColon:
|
|
|
|
case CodeCompletionString::CK_Equal:
|
|
|
|
case CodeCompletionString::CK_HorizontalSpace:
|
|
|
|
case CodeCompletionString::CK_VerticalSpace:
|
2010-01-06 03:32:54 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
// Note: treated as an empty text block.
|
|
|
|
return (*CCStr)[chunk_number].Optional;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be unreachable, but let's be careful.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
|
2010-01-06 03:32:54 +08:00
|
|
|
return CCStr? CCStr->size() : 0;
|
|
|
|
}
|
|
|
|
|
2010-05-27 06:00:08 +08:00
|
|
|
unsigned clang_getCompletionPriority(CXCompletionString completion_string) {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
|
2010-05-28 02:35:05 +08:00
|
|
|
return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely);
|
2010-05-27 06:00:08 +08:00
|
|
|
}
|
|
|
|
|
2010-08-24 07:00:57 +08:00
|
|
|
enum CXAvailabilityKind
|
|
|
|
clang_getCompletionAvailability(CXCompletionString completion_string) {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
|
|
|
|
return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability())
|
|
|
|
: CXAvailability_Available;
|
2010-08-24 07:00:57 +08:00
|
|
|
}
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
/// \brief The CXCodeCompleteResults structure we allocate internally;
|
|
|
|
/// the client only sees the initial CXCodeCompleteResults structure.
|
|
|
|
struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
|
2011-03-19 02:22:40 +08:00
|
|
|
AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts);
|
2010-02-19 07:07:20 +08:00
|
|
|
~AllocatedCXCodeCompleteResults();
|
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
/// \brief Diagnostics produced while performing code completion.
|
|
|
|
llvm::SmallVector<StoredDiagnostic, 8> Diagnostics;
|
|
|
|
|
2010-03-16 08:06:06 +08:00
|
|
|
/// \brief Diag object
|
2010-08-05 00:47:14 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<Diagnostic> Diag;
|
2010-03-16 08:06:06 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
/// \brief Language options used to adjust source locations.
|
2010-01-31 07:31:40 +08:00
|
|
|
LangOptions LangOpts;
|
2010-11-04 06:45:23 +08:00
|
|
|
|
|
|
|
FileSystemOptions FileSystemOpts;
|
|
|
|
|
2010-11-23 16:35:12 +08:00
|
|
|
/// \brief File manager, used for diagnostics.
|
|
|
|
FileManager FileMgr;
|
|
|
|
|
2010-11-04 06:45:23 +08:00
|
|
|
/// \brief Source manager, used for diagnostics.
|
|
|
|
SourceManager SourceMgr;
|
2010-02-19 07:35:40 +08:00
|
|
|
|
|
|
|
/// \brief Temporary files that should be removed once we have finished
|
|
|
|
/// with the code-completion results.
|
|
|
|
std::vector<llvm::sys::Path> TemporaryFiles;
|
2010-08-05 01:07:00 +08:00
|
|
|
|
2010-11-23 15:51:02 +08:00
|
|
|
/// \brief Temporary buffers that will be deleted once we have finished with
|
|
|
|
/// the code-completion results.
|
2010-08-05 01:07:00 +08:00
|
|
|
llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
|
2011-02-02 03:23:04 +08:00
|
|
|
|
2011-02-17 03:08:06 +08:00
|
|
|
/// \brief Allocator used to store globally cached code-completion results.
|
|
|
|
llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator>
|
|
|
|
CachedCompletionAllocator;
|
|
|
|
|
2011-02-02 03:23:04 +08:00
|
|
|
/// \brief Allocator used to store code completion results.
|
2011-02-02 07:14:23 +08:00
|
|
|
clang::CodeCompletionAllocator CodeCompletionAllocator;
|
2010-01-06 03:32:54 +08:00
|
|
|
};
|
|
|
|
|
2010-11-17 08:13:31 +08:00
|
|
|
/// \brief Tracks the number of code-completion result objects that are
|
|
|
|
/// currently active.
|
|
|
|
///
|
|
|
|
/// Used for debugging purposes only.
|
2010-12-07 08:05:48 +08:00
|
|
|
static llvm::sys::cas_flag CodeCompletionResultObjects;
|
2010-11-17 08:13:31 +08:00
|
|
|
|
2011-03-19 02:22:40 +08:00
|
|
|
AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults(
|
|
|
|
const FileSystemOptions& FileSystemOpts)
|
2010-11-19 04:06:41 +08:00
|
|
|
: CXCodeCompleteResults(),
|
|
|
|
Diag(new Diagnostic(
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))),
|
2011-03-19 02:22:40 +08:00
|
|
|
FileSystemOpts(FileSystemOpts),
|
2010-11-23 15:51:02 +08:00
|
|
|
FileMgr(FileSystemOpts),
|
2010-11-23 16:35:12 +08:00
|
|
|
SourceMgr(*Diag, FileMgr) {
|
2010-11-17 08:13:31 +08:00
|
|
|
if (getenv("LIBCLANG_OBJTRACKING")) {
|
2010-12-07 08:05:48 +08:00
|
|
|
llvm::sys::AtomicIncrement(&CodeCompletionResultObjects);
|
2010-11-17 08:13:31 +08:00
|
|
|
fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects);
|
|
|
|
}
|
|
|
|
}
|
2010-02-19 07:07:20 +08:00
|
|
|
|
|
|
|
AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
|
|
|
|
delete [] Results;
|
2010-02-19 07:35:40 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
|
|
|
|
TemporaryFiles[I].eraseFromDisk();
|
2010-08-05 01:07:00 +08:00
|
|
|
for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I)
|
|
|
|
delete TemporaryBuffers[I];
|
2010-11-17 08:13:31 +08:00
|
|
|
|
|
|
|
if (getenv("LIBCLANG_OBJTRACKING")) {
|
2010-12-07 08:05:48 +08:00
|
|
|
llvm::sys::AtomicDecrement(&CodeCompletionResultObjects);
|
2010-11-17 08:13:31 +08:00
|
|
|
fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects);
|
|
|
|
}
|
2010-02-19 07:07:20 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 00:47:14 +08:00
|
|
|
} // end extern "C"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CaptureCompletionResults : public CodeCompleteConsumer {
|
|
|
|
AllocatedCXCodeCompleteResults &AllocatedResults;
|
2010-10-12 05:37:58 +08:00
|
|
|
llvm::SmallVector<CXCompletionResult, 16> StoredResults;
|
2010-08-05 00:47:14 +08:00
|
|
|
public:
|
2011-02-02 03:23:04 +08:00
|
|
|
CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results)
|
2010-08-15 14:18:01 +08:00
|
|
|
: CodeCompleteConsumer(true, false, true, false),
|
|
|
|
AllocatedResults(Results) { }
|
2010-10-12 05:37:58 +08:00
|
|
|
~CaptureCompletionResults() { Finish(); }
|
|
|
|
|
2010-08-12 05:23:17 +08:00
|
|
|
virtual void ProcessCodeCompleteResults(Sema &S,
|
|
|
|
CodeCompletionContext Context,
|
2010-08-25 14:19:51 +08:00
|
|
|
CodeCompletionResult *Results,
|
2010-08-05 00:47:14 +08:00
|
|
|
unsigned NumResults) {
|
2010-10-12 05:37:58 +08:00
|
|
|
StoredResults.reserve(StoredResults.size() + NumResults);
|
2010-08-05 00:47:14 +08:00
|
|
|
for (unsigned I = 0; I != NumResults; ++I) {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *StoredCompletion
|
|
|
|
= Results[I].CreateCodeCompletionString(S,
|
|
|
|
AllocatedResults.CodeCompletionAllocator);
|
2010-10-12 05:37:58 +08:00
|
|
|
|
|
|
|
CXCompletionResult R;
|
|
|
|
R.CursorKind = Results[I].CursorKind;
|
|
|
|
R.CompletionString = StoredCompletion;
|
|
|
|
StoredResults.push_back(R);
|
2010-08-05 00:47:14 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-26 21:48:20 +08:00
|
|
|
|
2010-10-12 05:37:58 +08:00
|
|
|
virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
|
|
|
|
OverloadCandidate *Candidates,
|
|
|
|
unsigned NumCandidates) {
|
|
|
|
StoredResults.reserve(StoredResults.size() + NumCandidates);
|
|
|
|
for (unsigned I = 0; I != NumCandidates; ++I) {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *StoredCompletion
|
|
|
|
= Candidates[I].CreateSignatureString(CurrentArg, S,
|
|
|
|
AllocatedResults.CodeCompletionAllocator);
|
2010-10-12 05:37:58 +08:00
|
|
|
|
|
|
|
CXCompletionResult R;
|
|
|
|
R.CursorKind = CXCursor_NotImplemented;
|
|
|
|
R.CompletionString = StoredCompletion;
|
|
|
|
StoredResults.push_back(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-02 06:57:45 +08:00
|
|
|
virtual CodeCompletionAllocator &getAllocator() {
|
2011-02-02 03:23:04 +08:00
|
|
|
return AllocatedResults.CodeCompletionAllocator;
|
|
|
|
}
|
|
|
|
|
2010-10-12 05:37:58 +08:00
|
|
|
private:
|
|
|
|
void Finish() {
|
|
|
|
AllocatedResults.Results = new CXCompletionResult [StoredResults.size()];
|
|
|
|
AllocatedResults.NumResults = StoredResults.size();
|
|
|
|
std::memcpy(AllocatedResults.Results, StoredResults.data(),
|
|
|
|
StoredResults.size() * sizeof(CXCompletionResult));
|
|
|
|
StoredResults.clear();
|
|
|
|
}
|
2010-08-05 00:47:14 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
2010-08-20 07:44:10 +08:00
|
|
|
struct CodeCompleteAtInfo {
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
const char *complete_filename;
|
|
|
|
unsigned complete_line;
|
|
|
|
unsigned complete_column;
|
|
|
|
struct CXUnsavedFile *unsaved_files;
|
|
|
|
unsigned num_unsaved_files;
|
|
|
|
unsigned options;
|
|
|
|
CXCodeCompleteResults *result;
|
|
|
|
};
|
|
|
|
void clang_codeCompleteAt_Impl(void *UserData) {
|
|
|
|
CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
|
|
|
|
CXTranslationUnit TU = CCAI->TU;
|
|
|
|
const char *complete_filename = CCAI->complete_filename;
|
|
|
|
unsigned complete_line = CCAI->complete_line;
|
|
|
|
unsigned complete_column = CCAI->complete_column;
|
|
|
|
struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files;
|
|
|
|
unsigned num_unsaved_files = CCAI->num_unsaved_files;
|
|
|
|
unsigned options = CCAI->options;
|
|
|
|
CCAI->result = 0;
|
|
|
|
|
2010-08-05 00:47:14 +08:00
|
|
|
#ifdef UDP_CODE_COMPLETION_LOGGER
|
|
|
|
#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
|
|
|
|
const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
|
|
|
|
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData);
|
2010-08-05 00:47:14 +08:00
|
|
|
if (!AST)
|
2010-08-20 07:44:10 +08:00
|
|
|
return;
|
2010-08-05 00:47:14 +08:00
|
|
|
|
2010-09-24 02:47:53 +08:00
|
|
|
ASTUnit::ConcurrencyCheck Check(*AST);
|
|
|
|
|
2010-08-05 00:47:14 +08:00
|
|
|
// Perform the remapping of source files.
|
|
|
|
llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
|
|
|
|
for (unsigned I = 0; I != num_unsaved_files; ++I) {
|
|
|
|
llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
|
|
|
|
const llvm::MemoryBuffer *Buffer
|
|
|
|
= llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
|
|
|
|
RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
|
|
|
|
Buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnableLogging) {
|
|
|
|
// FIXME: Add logging.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the resulting source file to find code-completion results.
|
2011-03-19 02:22:40 +08:00
|
|
|
AllocatedCXCodeCompleteResults *Results =
|
|
|
|
new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts());
|
2010-08-05 00:47:14 +08:00
|
|
|
Results->Results = 0;
|
|
|
|
Results->NumResults = 0;
|
2011-02-17 03:08:06 +08:00
|
|
|
|
2010-08-05 00:47:14 +08:00
|
|
|
// Create a code-completion consumer to capture the results.
|
|
|
|
CaptureCompletionResults Capture(*Results);
|
|
|
|
|
|
|
|
// Perform completion.
|
|
|
|
AST->CodeComplete(complete_filename, complete_line, complete_column,
|
2010-08-05 17:09:23 +08:00
|
|
|
RemappedFiles.data(), RemappedFiles.size(),
|
|
|
|
(options & CXCodeComplete_IncludeMacros),
|
|
|
|
(options & CXCodeComplete_IncludeCodePatterns),
|
|
|
|
Capture,
|
2010-08-05 00:47:14 +08:00
|
|
|
*Results->Diag, Results->LangOpts, Results->SourceMgr,
|
2010-08-20 08:59:43 +08:00
|
|
|
Results->FileMgr, Results->Diagnostics,
|
|
|
|
Results->TemporaryBuffers);
|
2011-02-17 03:08:06 +08:00
|
|
|
|
|
|
|
// Keep a reference to the allocator used for cached global completions, so
|
|
|
|
// that we can be sure that the memory used by our code completion strings
|
|
|
|
// doesn't get freed due to subsequent reparses (while the code completion
|
|
|
|
// results are still active).
|
|
|
|
Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator();
|
2010-08-05 00:47:14 +08:00
|
|
|
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
|
2010-04-15 09:02:28 +08:00
|
|
|
#ifdef UDP_CODE_COMPLETION_LOGGER
|
|
|
|
#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
|
|
|
|
const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime();
|
|
|
|
llvm::SmallString<256> LogResult;
|
|
|
|
llvm::raw_svector_ostream os(LogResult);
|
|
|
|
|
|
|
|
// Figure out the language and whether or not it uses PCH.
|
|
|
|
const char *lang = 0;
|
|
|
|
bool usesPCH = false;
|
|
|
|
|
|
|
|
for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (*I == 0)
|
|
|
|
continue;
|
|
|
|
if (strcmp(*I, "-x") == 0) {
|
|
|
|
if (I + 1 != E) {
|
|
|
|
lang = *(++I);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(*I, "-include") == 0) {
|
|
|
|
if (I+1 != E) {
|
|
|
|
const char *arg = *(++I);
|
|
|
|
llvm::SmallString<512> pchName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream os(pchName);
|
|
|
|
os << arg << ".pth";
|
|
|
|
}
|
|
|
|
pchName.push_back('\0');
|
|
|
|
struct stat stat_results;
|
|
|
|
if (stat(pchName.data(), &stat_results) == 0)
|
|
|
|
usesPCH = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-17 08:21:42 +08:00
|
|
|
os << "{ ";
|
|
|
|
os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime());
|
|
|
|
os << ", \"numRes\": " << Results->NumResults;
|
|
|
|
os << ", \"diags\": " << Results->Diagnostics.size();
|
|
|
|
os << ", \"pch\": " << (usesPCH ? "true" : "false");
|
|
|
|
os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"';
|
2010-04-15 09:02:28 +08:00
|
|
|
const char *name = getlogin();
|
2010-04-17 08:21:42 +08:00
|
|
|
os << ", \"user\": \"" << (name ? name : "unknown") << '"';
|
|
|
|
os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
|
|
|
|
os << " }";
|
2010-04-15 09:02:28 +08:00
|
|
|
|
|
|
|
llvm::StringRef res = os.str();
|
|
|
|
if (res.size() > 0) {
|
|
|
|
do {
|
|
|
|
// Setup the UDP socket.
|
|
|
|
struct sockaddr_in servaddr;
|
|
|
|
bzero(&servaddr, sizeof(servaddr));
|
|
|
|
servaddr.sin_family = AF_INET;
|
|
|
|
servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT);
|
|
|
|
if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER,
|
|
|
|
&servaddr.sin_addr) <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sockfd < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sendto(sockfd, res.data(), res.size(), 0,
|
|
|
|
(struct sockaddr *)&servaddr, sizeof(servaddr));
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
while (false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2010-08-20 07:44:10 +08:00
|
|
|
CCAI->result = Results;
|
|
|
|
}
|
|
|
|
CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
|
|
|
|
const char *complete_filename,
|
|
|
|
unsigned complete_line,
|
|
|
|
unsigned complete_column,
|
|
|
|
struct CXUnsavedFile *unsaved_files,
|
|
|
|
unsigned num_unsaved_files,
|
|
|
|
unsigned options) {
|
|
|
|
CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line,
|
|
|
|
complete_column, unsaved_files, num_unsaved_files,
|
|
|
|
options, 0 };
|
|
|
|
llvm::CrashRecoveryContext CRC;
|
|
|
|
|
2010-11-05 15:19:31 +08:00
|
|
|
if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
|
2010-08-20 07:44:10 +08:00
|
|
|
fprintf(stderr, "libclang: crash detected in code completion\n");
|
2010-11-16 16:15:36 +08:00
|
|
|
static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
|
2010-08-20 07:44:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CCAI.result;
|
2010-01-06 03:32:54 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 17:09:23 +08:00
|
|
|
unsigned clang_defaultCodeCompleteOptions(void) {
|
|
|
|
return CXCodeComplete_IncludeMacros;
|
|
|
|
}
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
|
|
|
|
if (!ResultsIn)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AllocatedCXCodeCompleteResults *Results
|
|
|
|
= static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
|
|
|
|
delete Results;
|
|
|
|
}
|
2010-08-24 07:00:57 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
unsigned
|
|
|
|
clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
|
|
|
|
AllocatedCXCodeCompleteResults *Results
|
|
|
|
= static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
|
|
|
|
if (!Results)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Results->Diagnostics.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXDiagnostic
|
|
|
|
clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
|
|
|
|
unsigned Index) {
|
|
|
|
AllocatedCXCodeCompleteResults *Results
|
|
|
|
= static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
|
|
|
|
if (!Results || Index >= Results->Diagnostics.size())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
} // end extern "C"
|
2010-08-26 21:48:20 +08:00
|
|
|
|
2010-10-19 05:05:04 +08:00
|
|
|
/// \brief Simple utility function that appends a \p New string to the given
|
|
|
|
/// \p Old string, using the \p Buffer for storage.
|
|
|
|
///
|
|
|
|
/// \param Old The string to which we are appending. This parameter will be
|
|
|
|
/// updated to reflect the complete string.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// \param New The string to append to \p Old.
|
|
|
|
///
|
|
|
|
/// \param Buffer A buffer that stores the actual, concatenated string. It will
|
|
|
|
/// be used if the old string is already-non-empty.
|
|
|
|
static void AppendToString(llvm::StringRef &Old, llvm::StringRef New,
|
|
|
|
llvm::SmallString<256> &Buffer) {
|
|
|
|
if (Old.empty()) {
|
|
|
|
Old = New;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Buffer.empty())
|
|
|
|
Buffer.append(Old.begin(), Old.end());
|
|
|
|
Buffer.append(New.begin(), New.end());
|
|
|
|
Old = Buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the typed-text blocks from the given code-completion string
|
|
|
|
/// and return them as a single string.
|
|
|
|
///
|
|
|
|
/// \param String The code-completion string whose typed-text blocks will be
|
|
|
|
/// concatenated.
|
|
|
|
///
|
|
|
|
/// \param Buffer A buffer used for storage of the completed name.
|
|
|
|
static llvm::StringRef GetTypedName(CodeCompletionString *String,
|
|
|
|
llvm::SmallString<256> &Buffer) {
|
|
|
|
llvm::StringRef Result;
|
|
|
|
for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
|
|
|
|
C != CEnd; ++C) {
|
|
|
|
if (C->Kind == CodeCompletionString::CK_TypedText)
|
|
|
|
AppendToString(Result, C->Text, Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-08-26 21:48:20 +08:00
|
|
|
namespace {
|
|
|
|
struct OrderCompletionResults {
|
|
|
|
bool operator()(const CXCompletionResult &XR,
|
|
|
|
const CXCompletionResult &YR) const {
|
2011-02-02 03:23:04 +08:00
|
|
|
CodeCompletionString *X
|
|
|
|
= (CodeCompletionString *)XR.CompletionString;
|
|
|
|
CodeCompletionString *Y
|
|
|
|
= (CodeCompletionString *)YR.CompletionString;
|
2010-08-26 21:48:20 +08:00
|
|
|
|
2010-10-19 05:05:04 +08:00
|
|
|
llvm::SmallString<256> XBuffer;
|
|
|
|
llvm::StringRef XText = GetTypedName(X, XBuffer);
|
|
|
|
llvm::SmallString<256> YBuffer;
|
|
|
|
llvm::StringRef YText = GetTypedName(Y, YBuffer);
|
2010-08-26 21:48:20 +08:00
|
|
|
|
2010-10-19 05:05:04 +08:00
|
|
|
if (XText.empty() || YText.empty())
|
|
|
|
return !XText.empty();
|
|
|
|
|
|
|
|
int result = XText.compare_lower(YText);
|
2010-08-26 21:48:20 +08:00
|
|
|
if (result < 0)
|
|
|
|
return true;
|
|
|
|
if (result > 0)
|
|
|
|
return false;
|
|
|
|
|
2010-10-19 05:05:04 +08:00
|
|
|
result = XText.compare(YText);
|
2010-09-11 07:05:54 +08:00
|
|
|
return result < 0;
|
2010-08-26 21:48:20 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
void clang_sortCodeCompletionResults(CXCompletionResult *Results,
|
|
|
|
unsigned NumResults) {
|
|
|
|
std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
|
|
|
|
}
|
2010-08-26 23:07:07 +08:00
|
|
|
}
|