10
10
using ccls::Intern;
11
11
12
12
#include < clang/AST/AST.h>
13
- #include < clang/Frontend/ASTUnit.h>
14
- #include < clang/Frontend/CompilerInstance.h>
15
13
#include < clang/Frontend/FrontendAction.h>
16
14
#include < clang/Index/IndexDataConsumer.h>
17
15
#include < clang/Index/IndexingAction.h>
@@ -45,13 +43,10 @@ struct IndexParam {
45
43
};
46
44
std::unordered_map<const Decl *, DeclInfo> Decl2Info;
47
45
48
- ASTUnit &Unit;
49
46
ASTContext *Ctx;
50
-
51
47
FileConsumer *file_consumer = nullptr ;
52
48
53
- IndexParam (ASTUnit &Unit, FileConsumer *file_consumer)
54
- : Unit(Unit), file_consumer(file_consumer) {}
49
+ IndexParam (FileConsumer *file_consumer) : file_consumer(file_consumer) {}
55
50
56
51
IndexFile *ConsumeFile (const FileEntry &File) {
57
52
IndexFile *db = file_consumer->TryConsumeFile (File, &file_contents);
@@ -1158,14 +1153,9 @@ Index(VFS *vfs, const std::string &opt_wdir, const std::string &file,
1158
1153
if (!g_config->index .enabled )
1159
1154
return {};
1160
1155
1161
- std::vector<const char *> Args;
1162
- for (auto &arg : args)
1163
- Args.push_back (arg.c_str ());
1164
- auto PCHCO = std::make_shared<PCHContainerOperations>();
1165
- IntrusiveRefCntPtr<DiagnosticsEngine> Diags (
1166
- CompilerInstance::createDiagnostics (new DiagnosticOptions));
1167
- std::shared_ptr<CompilerInvocation> CI =
1168
- createInvocationFromCommandLine (Args, Diags);
1156
+ auto PCH = std::make_shared<PCHContainerOperations>();
1157
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem ();
1158
+ std::shared_ptr<CompilerInvocation> CI = BuildCompilerInvocation (args, FS);
1169
1159
if (!CI)
1170
1160
return {};
1171
1161
// -fparse-all-comments enables documentation in the indexer and in
@@ -1175,20 +1165,18 @@ Index(VFS *vfs, const std::string &opt_wdir, const std::string &file,
1175
1165
CI->getLangOpts ()->RetainCommentsFromSystemHeaders = true ;
1176
1166
CI->getLangOpts ()->SpellChecking = false ;
1177
1167
1178
- std::vector<std::unique_ptr<llvm::MemoryBuffer>> BufOwner;
1179
- for (auto &c : file_contents) {
1180
- std::unique_ptr<llvm::MemoryBuffer> MB =
1181
- llvm::MemoryBuffer::getMemBufferCopy (c.content , c.path );
1182
- CI->getPreprocessorOpts ().addRemappedFile (c.path , MB.get ());
1183
- BufOwner.push_back (std::move (MB));
1184
- }
1185
-
1186
- auto Unit = ASTUnit::create (CI, Diags, true , true );
1187
- if (!Unit)
1168
+ DiagnosticConsumer DC;
1169
+ auto Clang = std::make_unique<CompilerInstance>(PCH);
1170
+ Clang->setInvocation (std::move (CI));
1171
+ Clang->setVirtualFileSystem (FS);
1172
+ Clang->createDiagnostics (&DC, false );
1173
+ Clang->setTarget (TargetInfo::CreateTargetInfo (
1174
+ Clang->getDiagnostics (), Clang->getInvocation ().TargetOpts ));
1175
+ if (!Clang->hasTarget ())
1188
1176
return {};
1189
1177
1190
1178
FileConsumer file_consumer (vfs, file);
1191
- IndexParam param (*Unit, &file_consumer);
1179
+ IndexParam param (&file_consumer);
1192
1180
auto DataConsumer = std::make_shared<IndexDataConsumer>(param);
1193
1181
1194
1182
index ::IndexingOptions IndexOpts;
@@ -1199,36 +1187,30 @@ Index(VFS *vfs, const std::string &opt_wdir, const std::string &file,
1199
1187
IndexOpts.IndexImplicitInstantiation = true ;
1200
1188
#endif
1201
1189
1202
- std::unique_ptr<FrontendAction> IndexAction = createIndexingAction (
1190
+ std::unique_ptr<FrontendAction> Action = createIndexingAction (
1203
1191
DataConsumer, IndexOpts, std::make_unique<IndexFrontendAction>(param));
1204
1192
1205
- DiagnosticErrorTrap DiagTrap (*Diags);
1206
- llvm::CrashRecoveryContext CRC;
1207
- auto compile = [&]() {
1208
- ASTUnit::LoadFromCompilerInvocationAction (
1209
- std::move (CI), PCHCO, Diags, IndexAction.get (), Unit.get (),
1210
- /* Persistent=*/ true , /* ResourceDir=*/ " " ,
1211
- /* OnlyLocalDecls=*/ true ,
1212
- /* CaptureDiagnostics=*/ true , 0 , false , false ,
1213
- /* UserFilesAreVolatile=*/ true );
1214
- };
1215
- if (!CRC.RunSafely (compile)) {
1216
- LOG_S (ERROR) << " clang crashed for " << file;
1217
- return {};
1193
+ bool ok = false ;
1194
+ {
1195
+ llvm::CrashRecoveryContext CRC;
1196
+ auto parse = [&]() {
1197
+ if (!Action->BeginSourceFile (*Clang, Clang->getFrontendOpts ().Inputs [0 ]))
1198
+ return ;
1199
+ if (!Action->Execute ())
1200
+ return ;
1201
+ Action->EndSourceFile ();
1202
+ ok = true ;
1203
+ };
1204
+ if (!CRC.RunSafely (parse)) {
1205
+ LOG_S (ERROR) << " clang crashed for " << file;
1206
+ return {};
1207
+ }
1218
1208
}
1219
- if (!Unit ) {
1209
+ if (!ok ) {
1220
1210
LOG_S (ERROR) << " failed to index " << file;
1221
1211
return {};
1222
1212
}
1223
1213
1224
- const SourceManager &SM = Unit->getSourceManager ();
1225
- const FileEntry *FE = SM.getFileEntryForID (SM.getMainFileID ());
1226
- IndexFile *main_file = param.ConsumeFile (*FE);
1227
- std::unordered_map<std::string, int > inc_to_line;
1228
- if (main_file)
1229
- for (auto &inc : main_file->includes )
1230
- inc_to_line[inc.resolved_path ] = inc.line ;
1231
-
1232
1214
auto result = param.file_consumer ->TakeLocalState ();
1233
1215
for (std::unique_ptr<IndexFile> &entry : result) {
1234
1216
entry->import_file = file;
@@ -1251,22 +1233,6 @@ Index(VFS *vfs, const std::string &opt_wdir, const std::string &file,
1251
1233
for (auto &it : entry->usr2var )
1252
1234
Uniquify (it.second .uses );
1253
1235
1254
- if (main_file) {
1255
- // If there are errors, show at least one at the include position.
1256
- auto it = inc_to_line.find (entry->path );
1257
- if (it != inc_to_line.end ()) {
1258
- int line = it->second ;
1259
- for (auto ls_diagnostic : entry->diagnostics_ ) {
1260
- if (ls_diagnostic.severity != lsDiagnosticSeverity::Error)
1261
- continue ;
1262
- ls_diagnostic.range =
1263
- lsRange{lsPosition{line, 10 }, lsPosition{line, 10 }};
1264
- main_file->diagnostics_ .push_back (ls_diagnostic);
1265
- break ;
1266
- }
1267
- }
1268
- }
1269
-
1270
1236
// Update file contents and modification time.
1271
1237
entry->last_write_time = param.file2write_time [entry->path ];
1272
1238
0 commit comments