Eliminate clang_codeComplete(). libclang clients should be using the

faster, in-process, more-configurable clang_codeCompleteAt(). 

llvm-svn: 116245
This commit is contained in:
Douglas Gregor 2010-10-11 21:51:20 +00:00
parent 57feeed92f
commit 6a95802893
4 changed files with 1 additions and 389 deletions

View File

@ -2460,7 +2460,7 @@ clang_getCompletionAvailability(CXCompletionString completion_string);
* \brief Contains the results of code-completion. * \brief Contains the results of code-completion.
* *
* This data structure contains the results of code completion, as * This data structure contains the results of code completion, as
* produced by \c clang_codeComplete. Its contents must be freed by * produced by \c clang_codeCompleteAt(). Its contents must be freed by
* \c clang_disposeCodeCompleteResults. * \c clang_disposeCodeCompleteResults.
*/ */
typedef struct { typedef struct {
@ -2476,99 +2476,6 @@ typedef struct {
unsigned NumResults; unsigned NumResults;
} CXCodeCompleteResults; } CXCodeCompleteResults;
/**
* \brief Perform code completion at a given location in a source file.
*
* This function performs code completion at a particular file, line, and
* column within source code, providing results that suggest potential
* code snippets based on the context of the completion. The basic model
* for code completion is that Clang will parse a complete source file,
* performing syntax checking up to the location where code-completion has
* been requested. At that point, a special code-completion token is passed
* to the parser, which recognizes this token and determines, based on the
* current location in the C/Objective-C/C++ grammar and the state of
* semantic analysis, what completions to provide. These completions are
* returned via a new \c CXCodeCompleteResults structure.
*
* Code completion itself is meant to be triggered by the client when the
* user types punctuation characters or whitespace, at which point the
* code-completion location will coincide with the cursor. For example, if \c p
* is a pointer, code-completion might be triggered after the "-" and then
* after the ">" in \c p->. When the code-completion location is afer the ">",
* the completion results will provide, e.g., the members of the struct that
* "p" points to. The client is responsible for placing the cursor at the
* beginning of the token currently being typed, then filtering the results
* based on the contents of the token. For example, when code-completing for
* the expression \c p->get, the client should provide the location just after
* the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the
* client can filter the results based on the current token text ("get"), only
* showing those results that start with "get". The intent of this interface
* is to separate the relatively high-latency acquisition of code-completion
* results from the filtering of results on a per-character basis, which must
* have a lower latency.
*
* \param CIdx the \c CXIndex instance that will be used to perform code
* completion.
*
* \param source_filename the name of the source file that should be parsed to
* perform code-completion. This source file must be the same as or include the
* filename described by \p complete_filename, or no code-completion results
* will be produced. NOTE: One can also specify NULL for this argument if the
* source file is included in command_line_args.
*
* \param num_command_line_args the number of command-line arguments stored in
* \p command_line_args.
*
* \param command_line_args the command-line arguments to pass to the Clang
* compiler to build the given source file. This should include all of the
* necessary include paths, language-dialect switches, precompiled header
* includes, etc., but should not include any information specific to
* code completion.
*
* \param num_unsaved_files the number of unsaved file entries in \p
* unsaved_files.
*
* \param unsaved_files the files that have not yet been saved to disk
* but may be required for code completion, including the contents of
* those files. The contents and name of these files (as specified by
* CXUnsavedFile) are copied when necessary, so the client only needs to
* guarantee their validity until the call to this function returns.
*
* \param complete_filename the name of the source file where code completion
* should be performed. In many cases, this name will be the same as the
* source filename. However, the completion filename may also be a file
* included by the source file, which is required when producing
* code-completion results for a header.
*
* \param complete_line the line at which code-completion should occur.
*
* \param complete_column the column at which code-completion should occur.
* Note that the column should point just after the syntactic construct that
* initiated code completion, and not in the middle of a lexical token.
*
* \param diag_callback callback function that will receive any diagnostics
* emitted while processing this source file. If NULL, diagnostics will be
* suppressed.
*
* \param diag_client_data client data that will be passed to the diagnostic
* callback function.
*
* \returns if successful, a new CXCodeCompleteResults structure
* containing code-completion results, which should eventually be
* freed with \c clang_disposeCodeCompleteResults(). If code
* completion fails, returns NULL.
*/
CINDEX_LINKAGE
CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
const char *source_filename,
int num_command_line_args,
const char * const *command_line_args,
unsigned num_unsaved_files,
struct CXUnsavedFile *unsaved_files,
const char *complete_filename,
unsigned complete_line,
unsigned complete_column);
/** /**
* \brief Flags that can be passed to \c clang_codeCompleteAt() to * \brief Flags that can be passed to \c clang_codeCompleteAt() to
* modify its behavior. * modify its behavior.

View File

@ -218,16 +218,6 @@ clang_getCompletionAvailability(CXCompletionString completion_string) {
return CCStr? CCStr->getAvailability() : CXAvailability_Available; return CCStr? CCStr->getAvailability() : CXAvailability_Available;
} }
static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
unsigned &Value) {
if (Memory + sizeof(unsigned) > MemoryEnd)
return true;
memmove(&Value, Memory, sizeof(unsigned));
Memory += sizeof(unsigned);
return false;
}
/// \brief The CXCodeCompleteResults structure we allocate internally; /// \brief The CXCodeCompleteResults structure we allocate internally;
/// the client only sees the initial CXCodeCompleteResults structure. /// the client only sees the initial CXCodeCompleteResults structure.
struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
@ -271,289 +261,6 @@ AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
delete TemporaryBuffers[I]; delete TemporaryBuffers[I];
} }
CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
const char *source_filename,
int num_command_line_args,
const char * const *command_line_args,
unsigned num_unsaved_files,
struct CXUnsavedFile *unsaved_files,
const char *complete_filename,
unsigned complete_line,
unsigned complete_column) {
#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;
llvm::OwningPtr<llvm::NamedRegionTimer> CCTimer;
if (getenv("LIBCLANG_TIMING")) {
llvm::SmallString<128> TimerName;
llvm::raw_svector_ostream TimerNameOut(TimerName);
TimerNameOut << "Code completion (out-of-process) @ " << complete_filename
<< ":" << complete_line << ":" << complete_column;
CCTimer.reset(new llvm::NamedRegionTimer(TimerNameOut.str()));
}
// The indexer, which is mainly used to determine where diagnostics go.
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
// Configure the diagnostics.
DiagnosticOptions DiagOpts;
llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
// The set of temporary files that we've built.
std::vector<llvm::sys::Path> TemporaryFiles;
// Build up the arguments for invoking 'clang'.
std::vector<const char *> argv;
// First add the complete path to the 'clang' executable.
llvm::sys::Path ClangPath = CXXIdx->getClangPath();
argv.push_back(ClangPath.c_str());
// Always use Clang C++ support.
argv.push_back("-ccc-clang-cxx");
// Add the '-fsyntax-only' argument so that we only perform a basic
// syntax check of the code.
argv.push_back("-fsyntax-only");
// Add the appropriate '-code-completion-at=file:line:column' argument
// to perform code completion, with an "-Xclang" preceding it.
std::string code_complete_at;
code_complete_at += complete_filename;
code_complete_at += ":";
code_complete_at += llvm::utostr(complete_line);
code_complete_at += ":";
code_complete_at += llvm::utostr(complete_column);
argv.push_back("-Xclang");
argv.push_back("-code-completion-at");
argv.push_back("-Xclang");
argv.push_back(code_complete_at.c_str());
argv.push_back("-Xclang");
argv.push_back("-no-code-completion-debug-printer");
argv.push_back("-Xclang");
argv.push_back("-code-completion-macros");
argv.push_back("-fdiagnostics-binary");
// Remap any unsaved files to temporary files.
std::vector<std::string> RemapArgs;
if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
return 0;
// The pointers into the elements of RemapArgs are stable because we
// won't be adding anything to RemapArgs after this point.
for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
argv.push_back(RemapArgs[i].c_str());
// Add the source file name (FIXME: later, we'll want to build temporary
// file from the buffer, or just feed the source text via standard input).
if (source_filename)
argv.push_back(source_filename);
// Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
for (int i = 0; i < num_command_line_args; ++i)
if (const char *arg = command_line_args[i]) {
if (strcmp(arg, "-o") == 0) {
++i; // Also skip the matching argument.
continue;
}
if (strcmp(arg, "-emit-ast") == 0 ||
strcmp(arg, "-c") == 0 ||
strcmp(arg, "-fsyntax-only") == 0) {
continue;
}
// Keep the argument.
argv.push_back(arg);
}
if (EnableLogging) {
std::string Log = ClangPath.str();
for (unsigned I = 0, N = argv.size(); I != N; ++I) {
Log += ' ';
Log += argv[I];
}
fprintf(stderr, "libclang (Code Completion): %s\n", Log.c_str());
}
// Add the null terminator.
argv.push_back(NULL);
// Generate a temporary name for the code-completion results file.
char tmpFile[L_tmpnam];
char *tmpFileName = tmpnam(tmpFile);
llvm::sys::Path ResultsFile(tmpFileName);
TemporaryFiles.push_back(ResultsFile);
// Generate a temporary name for the diagnostics file.
char tmpFileResults[L_tmpnam];
char *tmpResultsFileName = tmpnam(tmpFileResults);
llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
TemporaryFiles.push_back(DiagnosticsFile);
// Invoke 'clang'.
llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
// on Unix or NUL (Windows).
std::string ErrMsg;
const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile,
&DiagnosticsFile, 0 };
llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
/* redirects */ &Redirects[0],
/* secondsToWait */ 0,
/* memoryLimits */ 0, &ErrMsg);
if (!ErrMsg.empty()) {
std::string AllArgs;
for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
I != E; ++I) {
AllArgs += ' ';
if (*I)
AllArgs += *I;
}
Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
}
// Parse the resulting source file to find code-completion results.
using llvm::MemoryBuffer;
using llvm::StringRef;
AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults;
Results->Results = 0;
Results->NumResults = 0;
// FIXME: Set Results->LangOpts!
if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) {
llvm::SmallVector<CXCompletionResult, 4> CompletionResults;
StringRef Buffer = F->getBuffer();
for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size();
Str < StrEnd;) {
unsigned KindValue;
if (ReadUnsigned(Str, StrEnd, KindValue))
break;
unsigned Priority;
if (ReadUnsigned(Str, StrEnd, Priority))
break;
unsigned Availability;
if (ReadUnsigned(Str, StrEnd, Availability))
break;
CXStoredCodeCompletionString *CCStr
= new CXStoredCodeCompletionString(Priority,
(CXAvailabilityKind)Availability);
if (!CCStr->Deserialize(Str, StrEnd)) {
delete CCStr;
continue;
}
if (!CCStr->empty()) {
// Vend the code-completion result to the caller.
CXCompletionResult Result;
Result.CursorKind = (CXCursorKind)KindValue;
Result.CompletionString = CCStr;
CompletionResults.push_back(Result);
}
};
// Allocate the results.
Results->Results = new CXCompletionResult [CompletionResults.size()];
Results->NumResults = CompletionResults.size();
memcpy(Results->Results, CompletionResults.data(),
CompletionResults.size() * sizeof(CXCompletionResult));
Results->TemporaryBuffers.push_back(F);
}
LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files,
Results->FileMgr, Results->SourceMgr,
Results->Diagnostics);
// Make sure we delete temporary files when the code-completion results are
// destroyed.
Results->TemporaryFiles.swap(TemporaryFiles);
#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;
}
}
}
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>") << '"';
const char *name = getlogin();
os << ", \"user\": \"" << (name ? name : "unknown") << '"';
os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
os << " }";
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
clang_sortCodeCompletionResults(Results->Results, Results->NumResults);
return Results;
}
} // end extern "C" } // end extern "C"
namespace { namespace {

View File

@ -1,6 +1,5 @@
_clang_CXXMethod_isStatic _clang_CXXMethod_isStatic
_clang_annotateTokens _clang_annotateTokens
_clang_codeComplete
_clang_codeCompleteAt _clang_codeCompleteAt
_clang_codeCompleteGetDiagnostic _clang_codeCompleteGetDiagnostic
_clang_codeCompleteGetNumDiagnostics _clang_codeCompleteGetNumDiagnostics

View File

@ -1,6 +1,5 @@
clang_CXXMethod_isStatic clang_CXXMethod_isStatic
clang_annotateTokens clang_annotateTokens
clang_codeComplete
clang_codeCompleteAt clang_codeCompleteAt
clang_codeCompleteGetDiagnostic clang_codeCompleteGetDiagnostic
clang_codeCompleteGetNumDiagnostics clang_codeCompleteGetNumDiagnostics