2017-02-16 09:35:30 +00:00
|
|
|
#include "TranslationUnit.h"
|
2017-04-22 07:39:55 +00:00
|
|
|
|
2017-02-16 09:35:30 +00:00
|
|
|
#include "Utility.h"
|
2017-03-29 06:33:38 +00:00
|
|
|
#include "../platform.h"
|
2017-04-22 07:39:55 +00:00
|
|
|
|
2017-02-16 09:35:30 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2017-02-22 08:52:00 +00:00
|
|
|
#include <cassert>
|
2017-03-25 19:18:25 +00:00
|
|
|
#include <iostream>
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
namespace clang {
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
TranslationUnit::TranslationUnit(
|
2017-04-18 04:06:01 +00:00
|
|
|
IndexerConfig* config,
|
2017-03-26 21:40:34 +00:00
|
|
|
Index &index,
|
|
|
|
const std::string& filepath,
|
|
|
|
const std::vector<std::string>& arguments,
|
|
|
|
std::vector<CXUnsavedFile> unsaved_files,
|
|
|
|
unsigned flags) {
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2017-02-16 09:35:30 +00:00
|
|
|
std::vector<const char*> args;
|
2017-03-29 06:33:38 +00:00
|
|
|
for (const std::string& a : arguments)
|
2017-03-25 19:18:25 +00:00
|
|
|
args.push_back(a.c_str());
|
2017-03-29 06:33:38 +00:00
|
|
|
|
|
|
|
std::vector<std::string> platform_args = GetPlatformClangArguments();
|
|
|
|
for (const auto& arg : platform_args)
|
|
|
|
args.push_back(arg.c_str());
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-04-18 04:06:01 +00:00
|
|
|
for (const std::string& arg : config->extraClangArguments)
|
|
|
|
args.push_back(arg.c_str());
|
|
|
|
|
2017-04-03 01:34:15 +00:00
|
|
|
std::cerr << "Parsing " << filepath << " with args ";
|
|
|
|
for (const auto& arg : args)
|
|
|
|
std::cerr << arg << " ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2017-02-22 08:52:00 +00:00
|
|
|
CXErrorCode error_code = clang_parseTranslationUnit2(
|
2017-03-26 21:40:34 +00:00
|
|
|
index.cx_index,
|
|
|
|
filepath.c_str(),
|
|
|
|
args.data(), args.size(),
|
|
|
|
unsaved_files.data(), unsaved_files.size(),
|
|
|
|
flags, &cx_tu);
|
2017-04-22 07:39:55 +00:00
|
|
|
|
2017-03-25 19:18:25 +00:00
|
|
|
switch (error_code) {
|
|
|
|
case CXError_Success:
|
2017-03-26 21:40:34 +00:00
|
|
|
did_fail = false;
|
2017-03-25 19:18:25 +00:00
|
|
|
break;
|
|
|
|
case CXError_Failure:
|
2017-03-26 21:40:34 +00:00
|
|
|
std::cerr << "libclang generic failure for " << filepath << std::endl;
|
|
|
|
did_fail = true;
|
2017-03-25 19:18:25 +00:00
|
|
|
break;
|
|
|
|
case CXError_Crashed:
|
2017-03-26 21:40:34 +00:00
|
|
|
std::cerr << "libclang crashed for " << filepath << std::endl;
|
|
|
|
did_fail = true;
|
2017-03-25 19:18:25 +00:00
|
|
|
break;
|
|
|
|
case CXError_InvalidArguments:
|
2017-03-26 21:40:34 +00:00
|
|
|
std::cerr << "libclang had invalid arguments for " << filepath << std::endl;
|
|
|
|
did_fail = true;
|
2017-03-25 19:18:25 +00:00
|
|
|
break;
|
|
|
|
case CXError_ASTReadError:
|
2017-03-26 21:40:34 +00:00
|
|
|
std::cerr << "libclang had ast read error for " << filepath << std::endl;
|
|
|
|
did_fail = true;
|
2017-03-25 19:18:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-02-16 09:35:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
TranslationUnit::~TranslationUnit() {
|
2017-02-16 09:35:30 +00:00
|
|
|
clang_disposeTranslationUnit(cx_tu);
|
|
|
|
}
|
|
|
|
|
2017-03-27 04:04:48 +00:00
|
|
|
void TranslationUnit::ReparseTranslationUnit(std::vector<CXUnsavedFile>& unsaved) {
|
|
|
|
int error_code = clang_reparseTranslationUnit(cx_tu, unsaved.size(), unsaved.data(), clang_defaultReparseOptions(cx_tu));
|
2017-03-26 21:40:34 +00:00
|
|
|
switch (error_code) {
|
|
|
|
case CXError_Success:
|
|
|
|
did_fail = false;
|
|
|
|
break;
|
|
|
|
case CXError_Failure:
|
|
|
|
std::cerr << "libclang reparse generic failure" << std::endl;
|
|
|
|
did_fail = true;
|
|
|
|
break;
|
|
|
|
case CXError_Crashed:
|
|
|
|
std::cerr << "libclang reparse crashed " << std::endl;
|
|
|
|
did_fail = true;
|
|
|
|
break;
|
|
|
|
case CXError_InvalidArguments:
|
|
|
|
std::cerr << "libclang reparse had invalid arguments" << std::endl;
|
|
|
|
did_fail = true;
|
|
|
|
break;
|
|
|
|
case CXError_ASTReadError:
|
|
|
|
std::cerr << "libclang reparse had ast read error" << std::endl;
|
|
|
|
did_fail = true;
|
|
|
|
break;
|
2017-02-16 09:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
Cursor TranslationUnit::document_cursor() const {
|
|
|
|
return Cursor(clang_getTranslationUnitCursor(cx_tu));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|