| 1 |
#include "CTags.hpp" |
| 2 |
|
| 3 |
#include "asyncprocess.h" |
| 4 |
#include "clTempFile.hpp" |
| 5 |
#include "cl_standard_paths.h" |
| 6 |
#include "ctags_manager.h" |
| 7 |
#include "file_logger.h" |
| 8 |
#include "fileutils.h" |
| 9 |
#include "procutils.h" |
| 10 |
#include "readtags.h" |
| 11 |
|
| 12 |
#include <set> |
| 13 |
#include <wx/stopwatch.h> |
| 14 |
#include <wx/tokenzr.h> |
| 15 |
|
| 16 |
namespace |
| 17 |
{ |
| 18 |
void WrapInShell(wxString& cmd) |
| 19 |
{ |
| 20 |
wxString command; |
| 21 |
#ifdef __WXMSW__ |
| 22 |
wxString shell = wxGetenv("COMSPEC"); |
| 23 |
if(shell.IsEmpty()) { |
| 24 |
shell = "CMD.EXE"; |
| 25 |
} |
| 26 |
command << shell << " /C "; |
| 27 |
if(cmd.StartsWith("\"") && !cmd.EndsWith("\"")) { |
| 28 |
command << "\"" << cmd << "\""; |
| 29 |
} else { |
| 30 |
command << cmd; |
| 31 |
} |
| 32 |
cmd = command; |
| 33 |
#else |
| 34 |
command << "/bin/sh -c '"; |
| 35 |
// escape any single quoutes |
| 36 |
cmd.Replace("'", "\\'"); |
| 37 |
command << cmd << "'"; |
| 38 |
cmd = command; |
| 39 |
#endif |
| 40 |
} |
| 41 |
} // namespace |
| 42 |
|
| 43 |
thread_local bool is_initialised = false; |
| 44 |
thread_local bool is_macrodef_supported = false; |
| 45 |
|
| 46 |
wxString CTags::WrapSpaces(const wxString& file) |
| 47 |
{ |
| 48 |
wxString fixed = file; |
| 49 |
if(fixed.Contains(" ")) { |
| 50 |
fixed.Prepend("\"").Append("\""); |
| 51 |
} |
| 52 |
return fixed; |
| 53 |
} |
| 54 |
namespace |
| 55 |
{ |
| 56 |
wxString fix_macro_entry(const wxString& macro) |
| 57 |
{ |
| 58 |
wxString fixed = macro; |
| 59 |
fixed.Replace("%", "__arg_"); |
| 60 |
//fixed.Replace("\"", "\\\""); |
| 61 |
return fixed; |
| 62 |
} |
| 63 |
} // namespace |
| 64 |
|
| 65 |
bool CTags::DoGenerate(const wxString& filesContent, const wxString& codelite_indexer, const wxStringMap_t& macro_table, |
| 66 |
const wxString& ctags_kinds, wxString* output) |
| 67 |
{ |
| 68 |
Initialise(codelite_indexer); |
| 69 |
clDEBUG() << "Generating ctags files" << clEndl; |
| 70 |
|
| 71 |
// prepare the options file |
| 72 |
// one option per line |
| 73 |
std::vector<wxString> options_arr; |
| 74 |
options_arr.reserve(500); |
| 75 |
wxString fields_cxx = "--fields-c++=+{template}+{properties}"; |
| 76 |
if(is_macrodef_supported) { |
| 77 |
fields_cxx << "+{macrodef}"; |
| 78 |
} |
| 79 |
|
| 80 |
options_arr = { "--extras=-p", "--excmd=pattern", "--sort=no", |
| 81 |
"--fields=aKmSsnit", "--language-force=c++", fields_cxx }; |
| 82 |
|
| 83 |
wxString kinds_string; |
| 84 |
if(ctags_kinds.empty()) { |
| 85 |
// default |
| 86 |
kinds_string << " --c-kinds=+pxz --C++-kinds=+pxz "; |
| 87 |
} else { |
| 88 |
kinds_string << " --c-kinds=" << ctags_kinds << " --C++-kinds=" << ctags_kinds << " "; |
| 89 |
} |
| 90 |
|
| 91 |
// we want the macros ordered, so we push them into std::set |
| 92 |
std::set<wxString> macros; |
| 93 |
for(const auto& vt : macro_table) { |
| 94 |
wxString macro_replacements; |
| 95 |
wxString fixed_macro_name = fix_macro_entry(vt.first); |
| 96 |
wxString fixed_macro_value = fix_macro_entry(vt.second); |
| 97 |
|
| 98 |
if(fixed_macro_value.empty()) { |
| 99 |
// simple -D |
| 100 |
macro_replacements << "-D" << fixed_macro_name; |
| 101 |
} else { |
| 102 |
wxString fixed_macro_name = fix_macro_entry(vt.first); |
| 103 |
wxString fixed_macro_value = fix_macro_entry(vt.second); |
| 104 |
macro_replacements << "-D" << fixed_macro_name << "=" << fixed_macro_value; |
| 105 |
} |
| 106 |
macros.insert(macro_replacements); |
| 107 |
} |
| 108 |
|
| 109 |
// write the options into a file |
| 110 |
wxFileName ctags_options_file(clStandardPaths::Get().GetUserDataDir(), |
| 111 |
wxString() << "options-" << wxThread::GetCurrentId() << ".ctags"); |
| 112 |
//FileUtils::Deleter d{ ctags_options_file }; |
| 113 |
|
| 114 |
wxString ctags_options_file_content; |
| 115 |
for(const wxString& option : options_arr) { |
| 116 |
ctags_options_file_content << option << "\n"; |
| 117 |
} |
| 118 |
|
| 119 |
// append the macros |
| 120 |
for(const auto& macro : macros) { |
| 121 |
ctags_options_file_content << macro << "\n"; |
| 122 |
} |
| 123 |
ctags_options_file_content.Trim(); |
| 124 |
FileUtils::WriteFileContent(ctags_options_file.GetFullPath(), ctags_options_file_content); |
| 125 |
|
| 126 |
// start timer |
| 127 |
wxStopWatch sw; |
| 128 |
|
| 129 |
sw.Start(); |
| 130 |
// Split the list of files |
| 131 |
wxFileName file_list(clStandardPaths::Get().GetTempDir(), |
| 132 |
wxString() << "file-list-" << wxThread::GetCurrentId() << ".txt"); |
| 133 |
FileUtils::WriteFileContent(file_list, filesContent); |
| 134 |
|
| 135 |
wxString command_to_run; |
| 136 |
command_to_run << WrapSpaces(codelite_indexer) << " --options=" << WrapSpaces(ctags_options_file.GetFullPath()) |
| 137 |
<< kinds_string << " -L " << WrapSpaces(file_list.GetFullPath()) << " -f - "; |
| 138 |
WrapInShell(command_to_run); |
| 139 |
clDEBUG() << "Running command:" << command_to_run << endl; |
| 140 |
|
| 141 |
*output = ProcUtils::SafeExecuteCommand(command_to_run); |
| 142 |
|
| 143 |
long elapsed = sw.Time(); |
| 144 |
|
| 145 |
clDEBUG() << "Parsing took:" << (elapsed / 1000) << "secs," << (elapsed % 1000) << "ms" << endl; |
| 146 |
clDEBUG() << "Generating ctags files... Success" << endl; |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
size_t CTags::ParseFiles(const std::vector<wxString>& files, const wxString& codelite_indexer, |
| 151 |
const wxStringMap_t& macro_table, std::vector<TagEntryPtr>& tags) |
| 152 |
{ |
| 153 |
wxString filesList; |
| 154 |
for(const auto& file : files) { |
| 155 |
filesList << file << "\n"; |
| 156 |
} |
| 157 |
wxString content; |
| 158 |
if(!DoGenerate(filesList, codelite_indexer, macro_table, wxEmptyString, &content)) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
tags.clear(); |
| 163 |
wxArrayString lines = ::wxStringTokenize(content, "\n", wxTOKEN_STRTOK); |
| 164 |
tags.reserve(lines.size()); |
| 165 |
|
| 166 |
// convert the lines into tags |
| 167 |
TagEntryPtr prev_scoped_tag = nullptr; |
| 168 |
for(wxString& line : lines) { |
| 169 |
line.Trim(false).Trim(); |
| 170 |
if(line.empty()) { |
| 171 |
continue; |
| 172 |
} |
| 173 |
|
| 174 |
// construct a tag from the line |
| 175 |
tags.emplace_back(new TagEntry()); |
| 176 |
auto tag = tags.back(); |
| 177 |
tag->FromLine(line); |
| 178 |
|
| 179 |
if(tag->IsEnumerator() // looking at an enumerator |
| 180 |
&& prev_scoped_tag // we have a previously seen scope |
| 181 |
&& prev_scoped_tag->GetFile() == tag->GetFile() /// and they are on the same file |
| 182 |
&& prev_scoped_tag->GetName() == tag->GetParent()) // and it belongs to it |
| 183 |
{ |
| 184 |
// remove one part of the scope |
| 185 |
wxArrayString scopes = ::wxStringTokenize(tag->GetScope(), ":", wxTOKEN_STRTOK); |
| 186 |
if(scopes.size()) { |
| 187 |
scopes.pop_back(); // remove the last part of the scope |
| 188 |
wxString new_scope; |
| 189 |
for(const wxString& scope : scopes) { |
| 190 |
if(!new_scope.empty()) { |
| 191 |
new_scope << "::"; |
| 192 |
} |
| 193 |
new_scope << scope; |
| 194 |
} |
| 195 |
// update the scope |
| 196 |
tag->SetScope(new_scope.empty() ? "<global>" : new_scope); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if(tag->IsEnum()) { |
| 201 |
prev_scoped_tag = tag; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if(tags.empty()) { |
| 206 |
clDEBUG() << "0 tags, ctags output:" << content << endl; |
| 207 |
} |
| 208 |
return tags.size(); |
| 209 |
} |
| 210 |
|
| 211 |
size_t CTags::ParseFile(const wxString& file, const wxString& codelite_indexer, const wxStringMap_t& macro_table, |
| 212 |
std::vector<TagEntryPtr>& tags) |
| 213 |
{ |
| 214 |
return ParseFiles({ file }, codelite_indexer, macro_table, tags); |
| 215 |
} |
| 216 |
|
| 217 |
size_t CTags::ParseBuffer(const wxFileName& filename, const wxString& buffer, const wxString& codelite_indexer, |
| 218 |
const wxStringMap_t& macro_table, std::vector<TagEntryPtr>& tags) |
| 219 |
{ |
| 220 |
// create a temporary file with the content we want to parse |
| 221 |
clTempFile temp_file("cpp"); |
| 222 |
temp_file.Write(buffer); |
| 223 |
// parse the file |
| 224 |
ParseFile(temp_file.GetFileName().GetFullPath(), codelite_indexer, macro_table, tags); |
| 225 |
// set the file name to the correct file |
| 226 |
for(TagEntryPtr tag : tags) { |
| 227 |
tag->SetFile(filename.GetFullPath()); |
| 228 |
} |
| 229 |
return tags.size(); |
| 230 |
} |
| 231 |
|
| 232 |
size_t CTags::ParseLocals(const wxFileName& filename, const wxString& buffer, const wxString& codelite_indexer, |
| 233 |
const wxStringMap_t& macro_table, std::vector<TagEntryPtr>& tags) |
| 234 |
{ |
| 235 |
wxString content; |
| 236 |
{ |
| 237 |
clTempFile temp_file("cpp"); |
| 238 |
temp_file.Write(buffer); |
| 239 |
|
| 240 |
wxString filesList; |
| 241 |
filesList << temp_file.GetFullPath() << "\n"; |
| 242 |
|
| 243 |
// we want locals + functions (to resolve the scope) |
| 244 |
if(!DoGenerate(filesList, codelite_indexer, macro_table, "lzpvfm", &content)) { |
| 245 |
return 0; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
tags.clear(); |
| 250 |
wxArrayString lines = ::wxStringTokenize(content, "\n", wxTOKEN_STRTOK); |
| 251 |
tags.reserve(lines.size()); |
| 252 |
|
| 253 |
// convert the lines into tags |
| 254 |
for(auto& line : lines) { |
| 255 |
line.Trim().Trim(false); |
| 256 |
if(line.empty()) { |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
// construct a tag from the line |
| 261 |
tags.emplace_back(new TagEntry()); |
| 262 |
auto tag = tags.back(); |
| 263 |
tag->FromLine(line); |
| 264 |
tag->SetFile(filename.GetFullPath()); |
| 265 |
} |
| 266 |
|
| 267 |
if(tags.empty()) { |
| 268 |
clDEBUG() << "0 local tags, ctags output:" << content << endl; |
| 269 |
} |
| 270 |
return tags.size(); |
| 271 |
} |
| 272 |
|
| 273 |
void CTags::Initialise(const wxString& codelite_indexer) |
| 274 |
{ |
| 275 |
if(is_initialised) { |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
is_initialised = true; |
| 280 |
// check whether we have `macrodef` supported |
| 281 |
wxString output; |
| 282 |
std::vector<wxString> command = { codelite_indexer, "--list-fields=c++" }; |
| 283 |
auto process = ::CreateAsyncProcess(nullptr, command, IProcessCreateSync, wxEmptyString, nullptr, wxEmptyString); |
| 284 |
if(process) { |
| 285 |
process->WaitForTerminate(output); |
| 286 |
} |
| 287 |
|
| 288 |
wxArrayString lines = ::wxStringTokenize(output, "\n", wxTOKEN_STRTOK); |
| 289 |
for(const auto& line : lines) { |
| 290 |
if(line.Contains("macrodef")) { |
| 291 |
is_macrodef_supported = true; |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
} |