1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
/*
Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "cpputils.h"
#include "setuphelpers.h"
#include "parser/rpp/preprocessor.h"
#include "includepathcomputer.h"
#include <interfaces/icore.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <language/codegen/coderepresentation.h>
#include <language/duchain/declaration.h>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/util/includeitem.h>
#include "interfaces/foregroundlock.h"
#include <project/projectmodel.h>
#include <QDirIterator>
#include <QThread>
#include <QCoreApplication>
template<class T>
static QList<T> makeListUnique(const QList<T>& list)
{
QList<T> ret;
QSet<QString> set;
foreach(const T& item, list)
{
if(!set.contains(item))
{
ret << item;
set.insert(item);
}
}
return ret;
}
QString addDot(QString ext) {
if(ext.contains('.')) //We need this check because of the _impl.h thing
return ext;
else
return "." + ext;
}
using namespace KDevelop;
namespace CppUtils
{
int findEndOfInclude(QString line)
{
QString tmp = line;
tmp = tmp.trimmed();
if(!tmp.startsWith("#"))
return -1;
tmp = tmp.mid(1).trimmed();
if(!tmp.startsWith("include"))
return -1;
return line.indexOf("include") + 7;
}
QString sourceOrHeaderCandidate( const QString &path_, bool fast )
{
// get the path of the currently active document
QFileInfo fi( path_ );
QString path = fi.filePath();
// extract the exension
QString ext = fi.suffix();
if ( ext.isEmpty() )
return QString();
// extract the base path (full path without '.' and extension)
QString base = path.left( path.length() - ext.length() - 1 );
//kDebug( 9007 ) << "base: " << base << ", ext: " << ext << endl;
// just the filename without the extension
QString fileNameWoExt = fi.fileName();
if ( !ext.isEmpty() )
fileNameWoExt.replace( "." + ext, "" );
QStringList possibleExts;
// depending on the current extension assemble a list of
// candidate files to look for
QStringList candidates;
// special case for template classes created by the new class dialog
if ( path.endsWith( "_impl.h" ) )
{
QString headerpath = path;
headerpath.replace( "_impl.h", ".h" );
candidates << headerpath;
fileNameWoExt.replace( "_impl", "" );
possibleExts << "h";
}
// if file is a header file search for implementation file
else if ( headerExtensions().contains( ext ) )
{
foreach(const QString& ext, sourceExtensions())
candidates << ( base + addDot(ext) );
possibleExts = sourceExtensions();
}
// if file is an implementation file, search for header file
else if ( sourceExtensions().contains( ext ) )
{
foreach(const QString& ext, headerExtensions())
candidates << ( base + addDot(ext) );
possibleExts = headerExtensions();
}
// search for files from the assembled candidate lists, return the first
// candidate file that actually exists or QString::null if nothing is found.
QStringList::ConstIterator it;
for ( it = candidates.constBegin(); it != candidates.constEnd(); ++it )
{
// kDebug( 9007 ) << "Trying " << ( *it ) << endl;
if ( QFileInfo( *it ).exists() )
{
// kDebug( 9007 ) << "using: " << *it << endl;
return * it;
}
}
if(fast)
return QString();
//kDebug( 9007 ) << "Now searching in project files." << endl;
// Our last resort: search the project file list for matching files
QFileInfo candidateFileWoExt;
QString candidateFileWoExtString;
const IndexedString file(path);
foreach (KDevelop::IProject *project, ICore::self()->projectController()->projects()) {
if (project->inProject(file)) {
foreach(const IndexedString& otherFile, project->fileSet()) {
candidateFileWoExt.setFile(otherFile.str());
//kDebug( 9007 ) << "candidate file: " << otherFile.str() << endl;
if( !candidateFileWoExt.suffix().isEmpty() )
candidateFileWoExtString = candidateFileWoExt.fileName().replace( "." + candidateFileWoExt.suffix(), "" );
if ( candidateFileWoExtString == fileNameWoExt )
{
if ( possibleExts.contains( candidateFileWoExt.suffix() ) || candidateFileWoExt.suffix().isEmpty() )
{
//kDebug( 9007 ) << "checking if " << url << " exists" << endl;
return otherFile.str();
}
}
}
}
}
return QString();
}
bool isHeader(const KUrl &url) {
QFileInfo fi( url.toLocalFile() );
QString path = fi.filePath();
// extract the exension
QString ext = fi.suffix();
if ( ext.isEmpty() )
return true;
return headerExtensions().contains(ext);
}
const Cpp::ReferenceCountedMacroSet& standardMacros()
{
static Cpp::ReferenceCountedMacroSet macros = CppTools::setupStandardMacros();
return macros;
}
QPair<Path, Path> findInclude(const Path::List& includePaths, const Path& localPath,
const QString& includeName, int includeType,
const Path& skipPath, bool quiet){
QPair<Path, Path> ret;
#ifdef DEBUG
kDebug(9007) << "searching for include-file" << includeName;
if( !skipPath.isEmpty() )
kDebug(9007) << "skipping path" << skipPath;
#endif
if (includeName.startsWith('/')) {
QFileInfo info(includeName);
if (info.exists() && info.isReadable() && info.isFile()) {
//kDebug(9007) << "found include file:" << info.absoluteFilePath();
ret.first = Path(info.canonicalFilePath());
ret.second = Path("/");
return ret;
}
}
if (includeType == rpp::Preprocessor::IncludeLocal && localPath != skipPath) {
Path check(localPath, includeName);
QFileInfo info(check.toLocalFile());
if (info.exists() && info.isReadable() && info.isFile()) {
//kDebug(9007) << "found include file:" << info.absoluteFilePath();
ret.first = check;
ret.second = localPath;
return ret;
}
}
//When a path is skipped, we will start searching exactly after that path
bool needSkip = skipPath.isValid();
restart:
foreach( const Path& path, includePaths ) {
if( needSkip ) {
if( path == skipPath ) {
needSkip = false;
continue;
}
}
Path check(path, includeName);
QFileInfo info(check.toLocalFile());
if (info.exists() && info.isReadable() && info.isFile()) {
//kDebug(9007) << "found include file:" << info.absoluteFilePath();
ret.first = check;
ret.second = path;
return ret;
}
}
if( needSkip ) {
//The path to be skipped was not found, so simply start from the begin, considering any path.
needSkip = false;
goto restart;
}
const int idx = includeName.indexOf('/');
if ( idx != -1 ) {
// HACK: parse Qt4 includes and similar even without the full include paths from the project manager
// there, a file in /usr/include/qt4/QtCore/ tries to include sibling files via QtCore/file
ret = findInclude(includePaths, localPath, includeName.mid(idx + 1), rpp::Preprocessor::IncludeLocal, skipPath, false);
}
if( !ret.first.isValid())
{
//Check if there is an available artificial representation
if(!includeName.isNull() && artificialCodeRepresentationExists(IndexedString(includeName)))
{
ret.first = Path(CodeRepresentation::artificialPath(includeName));
kDebug() << "Utilizing Artificial code for include: " << includeName;
}
else if(!quiet ) {
kDebug() << "FAILED to find include-file" << includeName << "in paths:" << includePaths;
}
}
return ret;
}
bool needsUpdate(const Cpp::EnvironmentFilePointer& file, const Path& localPath, const Path::List& includePaths)
{
if(file->needsUpdate())
return true;
///@todo somehow this check should also go into EnvironmentManager
for( Cpp::ReferenceCountedStringSet::Iterator it = file->missingIncludeFiles().iterator(); it; ++it ) {
///@todo store IncludeLocal/IncludeGlobal somewhere
auto included = findInclude( includePaths, localPath, (*it).str(), rpp::Preprocessor::IncludeLocal, Path(), true );
if(included.first.isValid())
return true;
}
return false;
}
/**
* This is an ugly HACK but well... it works and doesn't require an extremely big API refactoring.
*
* Thing is, the findIncludePaths was potentially called from background threads, mostly during
* code completion. From there, it is not safe to call IncludePathComputer::computeForeground.
* To fix this, we now add another eventloop roundtrip, execute that function in the foreground
* and then afterwards compute the rest in the background. This does mean that the background thread
* is _blocked_ while we compute stuff in the foreground.
*
* This must be properly fixed when we port the MissingInclude feature to clang. There, the
* include paths must be queried in the foreground thread, before invoking code completion.
*/
class IncludePathForegroundComputer : public DoInForeground
{
public:
IncludePathForegroundComputer(IncludePathComputer* includePathComputer)
: m_includePathComputer(includePathComputer)
{}
private:
void doInternal() override final
{
m_includePathComputer->computeForeground();
}
IncludePathComputer* m_includePathComputer;
};
Path::List findIncludePaths(const QString& source)
{
Q_ASSERT(QThread::currentThread() == qApp->thread() ||
(!DUChain::lock()->currentThreadHasReadLock() && !DUChain::lock()->currentThreadHasWriteLock()));
IncludePathComputer comp(source);
IncludePathForegroundComputer foreground(&comp);
foreground.doIt();
comp.computeBackground();
return comp.result();
}
QList<KDevelop::IncludeItem> allFilesInIncludePath(const QString& source, bool local, const QString& addPath,
const QStringList& addIncludePaths, bool onlyAddedIncludePaths,
bool prependAddedPathToName, bool allowSourceFiles)
{
QList<KDevelop::IncludeItem> ret;
QStringList paths;
if ( addPath.startsWith('/') ) {
paths << QString("/");
} else {
paths = addIncludePaths;
if(!onlyAddedIncludePaths) {
foreach(const Path& path, findIncludePaths(source)) {
paths += path.toLocalFile();
}
if(local) {
KUrl localPath(source);
localPath.setFileName(QString());
paths.push_front(localPath.toLocalFile());
}
}
}
paths = makeListUnique<QString>(paths);
int pathNumber = 0;
QSet<QString> hadIncludePaths;
foreach(const QString& path, paths)
{
if(hadIncludePaths.contains(path)) {
continue;
}
hadIncludePaths.insert(path);
QString searchPath = path;
if (!addPath.isEmpty() && !addPath.startsWith('/')) {
if (!searchPath.endsWith('/')) {
searchPath += '/';
}
searchPath += addPath;
}
QDirIterator dirContent(searchPath);
while(dirContent.hasNext()) {
dirContent.next();
KDevelop::IncludeItem item;
item.name = dirContent.fileName();
if(item.name.startsWith('.') || item.name.endsWith("~")) //This filters out ".", "..", and hidden files, and backups
continue;
QString suffix = dirContent.fileInfo().suffix();
if(!dirContent.fileInfo().suffix().isEmpty() && !headerExtensions().contains(suffix) && (!allowSourceFiles || !sourceExtensions().contains(suffix)))
continue;
QString fullPath = dirContent.fileInfo().canonicalFilePath();
if (hadIncludePaths.contains(fullPath)) {
continue;
} else {
hadIncludePaths.insert(fullPath);
}
if(prependAddedPathToName) {
item.name = addPath + item.name;
item.basePath = path;
} else {
item.basePath = searchPath;
}
item.isDirectory = dirContent.fileInfo().isDir();
item.pathNumber = pathNumber;
ret << item;
}
++pathNumber;
}
return ret;
}
QStringList headerExtensions()
{
static const QStringList headerExtensions = QString("h,H,hh,hxx,hpp,tlh,h++").split(',');
return headerExtensions;
}
QStringList sourceExtensions()
{
static const QStringList sourceExtensions = QString("c,cc,cpp,c++,cxx,C,m,mm,M,inl,_impl.h").split(',');
return sourceExtensions;
}
}
|