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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/* This file is part of KDevelop
SPDX-FileCopyrightText: 2002, 2003 Roberto Raggi <roberto@kdevelop.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef DRIVER_H
#define DRIVER_H
#include "ast.h"
#include "macro.h"
#include <qpair.h>
#include <QStringList>
#include <qdatastream.h>
#include <qmap.h>
#include <qdatetime.h>
#include <QList>
#include <map>
#include <set>
#include <hashedstring.h>
#include "lexercache.h"
class Lexer;
class Parser;
enum {
Dep_Global,
Dep_Local
};
typedef QPair<QString, int> Dependence;
class ParsedFile;
typedef QExplicitlySharedDataPointer<ParsedFile> ParsedFilePointer;
class ParsedFile: public AbstractParseResult
{
public:
struct IncludeDesc {
bool local; //Whether it is a local include(#include "local.h", not #include <global.h>)
QString includePath;
ParsedFilePointer parsed; //May be zero!
};
// ParsedFile() {
// }
explicit ParsedFile(QDataStream& s)
{
read(s);
}
ParsedFile(const QString& fileName, const QDateTime& timeStamp);
///Deserializes the ParsedFile from a previous call to serialize(). AST will always be zero after a call to this.
explicit ParsedFile(const QByteArray& array);
virtual ~ParsedFile() {}
/**
* @return All Macros that were used while processing this translation-unit. May be modified.
*/
MacroSet& usedMacros();
const MacroSet& usedMacros() const;
/**
* @return the count of lines that were skipped while preprocessing the file
* */
int skippedLines() const;
void setSkippedLines(int lines);
/**
* @return Absolutely all files included by this one(no matter through how many other files they were included)
*/
// HashedStringSet& includeFiles();
const HashedStringSet& includeFiles() const;
void addIncludeFiles(const HashedStringSet& includeFiles);
void addIncludeFile(const QString& includePath, const ParsedFilePointer& parsed, bool localInclude);
///If this file was parsed while resolving the dependencies of another file, this returns the file this one was included from. Else returns an empty string.
QString includedFrom() const;
void setIncludedFrom(const QString& str);
/**
* @return Reference to the internal list of all directly included files(without those included indirectly)
*/
const QList<IncludeDesc>& directIncludeFiles() const;
operator TranslationUnitAST* () const // May be zero
{
if (!this) return nullptr;
return (TranslationUnitAST*)m_translationUnit.data();
}
TranslationUnitAST* operator -> () const
{
return (TranslationUnitAST*)m_translationUnit.data();
}
void setTranslationUnit(const TranslationUnitAST::Node& trans);
QString fileName() const;
QDateTime timeStamp() const;
///Serializes the content of this class into a byte-array. Note that this does not serialize the AST.
QByteArray serialize() const;
/*void read(QDataStream& stream);
void write(QDataStream& stream) const;*/
virtual void read(QDataStream& stream)
{
int directIncludeFilesCount;
stream >> directIncludeFilesCount;
m_directIncludeFiles.clear();
for (int a = 0; a < directIncludeFilesCount; a++) {
IncludeDesc i;
Q_INT8 in;
stream >> in;
i.local = in;
stream >> i.includePath;
//"parsed" will not be reconstructed
m_directIncludeFiles.push_back(i);
}
stream >> m_skippedLines;
stream >> m_fileName;
stream >> m_timeStamp;
stream >> m_includedFrom;
m_usedMacros.read(stream);
m_translationUnit = nullptr;
m_includeFiles.read(stream);
}
virtual void write(QDataStream& stream) const
{
int i = m_directIncludeFiles.size();
stream << i;
for (QList<IncludeDesc>::const_iterator it = m_directIncludeFiles.begin(); it != m_directIncludeFiles.end(); ++it) {
Q_INT8 i = (*it).local;
stream << i;
stream << (*it).includePath;
}
stream << m_skippedLines;
stream << m_fileName;
stream << m_timeStamp;
stream << m_includedFrom;
m_usedMacros.write(stream);
m_includeFiles.write(stream);
}
virtual ParsedFileType type() const
{
return CppParsedFile;
}
private:
QList<IncludeDesc> m_directIncludeFiles;
MacroSet m_usedMacros;
TranslationUnitAST::Node m_translationUnit;
HashedStringSet m_includeFiles;
int m_skippedLines;
QString m_fileName;
QDateTime m_timeStamp;
QString m_includedFrom;
};
/**
* An interface that provides source code to the Driver
*/
class SourceProvider
{
public:
SourceProvider() {}
virtual ~SourceProvider() {}
/**
* Get the contents of a file
* \param fileName The name of the file to get the contents for. An absolute
* path should be used.
* \return A QString that contains the contents of the file
*/
virtual QString contents(const QString& fileName) = 0;
/**
* Check to see if a file has been modified
* \param fileName The name of hte file to get the modification state of. An
* absolute path should be used.
* \return true if the file has been modified
* \return false if the file has not been modified
*/
virtual bool isModified(const QString& fileName) = 0;
private:
SourceProvider(const SourceProvider& source);
void operator = (const SourceProvider& source);
};
/**
* @brief The Driver class takes care of the management of the include files,
* the macros, calls the parser and stores errors that occurred during parsing.
*/
class Driver
{
public:
Driver();
virtual ~Driver();
typedef std::multimap< HashedString, Macro > MacroMap;
/**
* Get the source provider for this driver. This would be useful for
* getting the text the driver is working with.
*/
SourceProvider* sourceProvider();
/**
* Sets the source provider the driver will use
* @param sourceProvider the SourceProvider the driver will use
*/
void setSourceProvider(SourceProvider* sourceProvider);
/**
* @brief Resets the driver
*
* Clears the driver of all problems, dependencies, macros, and include paths and
* removes any translation units that have been parsed
*/
virtual void reset();
/**
* Tells the driver to start parsing a file
* @param fileName The name of the file to parse
* @param onlyPreProcesss Tells the driver to only run the file through the preprocessor. Defaults to false
* @param force Force the parsing of the file. Defaults to false
* @param macrosGlobal Should the macros be global? (Global macros are not deleted once a new translation-unit is parsed)
* @return true on success
*/
virtual bool parseFile(const QString& fileName, bool onlyPreProcesss = false, bool force = false, bool macrosGlobal = false);
/**
* Indicates that the file has been parsed
* @param fileName The name of the file parsed. It is legal to create a ParsedFilePointer on the given item.
*/
virtual void fileParsed(ParsedFile& fileName);
/**
* Removes the file specified by @p fileName from the driver
* @param fileName The name of the file to remove
*/
virtual void remove
(const QString& fileName);
/**
* Add a dependency on another header file for @p fileName
* @param fileName The file name to add the dependency for
* @param dep The dependency to add
*/
virtual void addDependence(const QString& fileName, const Dependence& dep);
/**
* Add a macro to the driver
* @param macro The macro to add to the driver
*/
virtual void addMacro(const Macro& macro);
/**
* Add a problem to the driver
* @param fileName The file name to add the problem for
* @param problem The problem to add
*/
virtual void addProblem(const QString& fileName, const Problem& problem);
/**
* The current file name the driver is working with
*/
QString currentFileName() const
{
return m_currentFileName;
}
ParsedFilePointer takeTranslationUnit(const QString& fileName);
void takeTranslationUnit(const ParsedFile& file);
/**
* Get the translation unit contained in the driver for @p fileName.
* @param fileName The name of the file to get the translation unit for
* @return The TranslationUnitAST pointer that represents the translation unit
* @return 0 if no translation unit exists for the file
*/
ParsedFilePointer translationUnit(const QString& fileName) const;
/**
* Get the dependencies for a file
* @param fileName The file name to get dependencies for
* @return The dependencies for the file
*/
QMap<QString, Dependence> dependences(const QString& fileName) const;
/**
* Get all the macros the driver contains
* @return The macros
*/
const MacroMap& macros() const;
/**
* Take all macros from the given map(forgetting own macros) */
void insertMacros(const MacroSet& macros);
/**
* Get the list of problem areas the driver contains
* @param fileName The filename to get problems for
* @return The list of problems for @p fileName
*/
QList<Problem> problems(const QString& fileName) const;
void usingString(const HashedString& str);
/**
* Check if we have a macro in the driver
* If the last stacked macro of that name is an undef-macro, false is returned.
* @param name The name of the macro to check for
* @return true if we have the macro in the driver
* @return false if we don't have the macro in the driver
*/
bool hasMacro(const HashedString& name) ;
/**
* Get the macro identified by @p name
* @param name The name of the macro to get
* @return A const reference of the macro object represented by @p name
*/
const Macro& macro(const HashedString& name) const;
/**
* Get the last inserted macro identified by @p name
* @override
* @param name The name of the macro to get
* @return A non-const reference of the macro object represented by @p name
*
*/
Macro& macro(const HashedString& name);
/**
* Remove the last inserted Macro of that name
* @param macroName The name of the macro to remove
*/
virtual void removeMacro(const HashedString& macroName);
/**
* Remove all macros from the driver for a certain file
* @param fileName The file name
*/
virtual void removeAllMacrosInFile(const QString& fileName); ///Check when this is called. It may be wrong.
QStringList includePaths() const
{
return m_includePaths;
}
virtual QStringList getCustomIncludePath(const QString&);
virtual void addIncludePath(const QString &path);
virtual void clearIncludePaths();
/// @todo remove
const QMap<QString, ParsedFilePointer> &parsedUnits() const
{
return m_parsedUnits;
}
/**
* Set whether or not to enable dependency resolving for files added to the driver
*/
virtual void setResolveDependencesEnabled(bool enabled);
/**
* Check if dependency resolving is enabled
* \return true if dependency resolving is enabled
* \return false if dependency resolving is disabled
*/
bool isResolveDependencesEnabled() const
{
return depresolv;
}
void setMaxDependenceDepth(int depth);
/**
* Used by the Lexer to indicate that a Macro was used
* @param macro The used macro
* */
void usingMacro(const Macro& macro);
/**
* Returns the local instance of the lexer-cache, can be used from outside to control the cache-behavior.
* */
LexerCache* lexerCache();
///This uses getCustomIncludePath(..) to resolve the include-path internally
QString findIncludeFile(const Dependence& dep, const QString& fromFile);
protected:
///This uses the state of the parser to find the include-file
QString findIncludeFile(const Dependence& dep) const;
/**
* Set up the lexer.
*/
virtual void setupLexer(Lexer* lexer);
/**
* Setup the parser
*/
virtual void setupParser(Parser* parser);
/**
* Set up the preprocessor
*/
virtual void setupPreProcessor();
/**
* Is code-information for this file already available? If false is returned, the file will be parsed.
* Code-model and static repository should be checked to find out whether the file is already available.
* This function is only used when dependency-resolving is activated.
* @arg file absolute path to the file
*/
virtual bool shouldParseIncludedFile(const ParsedFilePointer& /*file*/);
void clearMacros();
void clearParsedMacros();
private:
QMap<QString, Dependence>& findOrInsertDependenceList(const QString& fileName);
QList<Problem>& findOrInsertProblemList(const QString& fileName);
private:
QString m_currentFileName;
QString m_currentMasterFileName;
typedef QMap<QString, Dependence> DependenceMap;
typedef QMap< QString, DependenceMap> DependencesMap;
DependencesMap m_dependences;
MacroMap m_macros;
QMap< QString, QList<Problem> > m_problems;
QMap<QString, ParsedFilePointer> m_parsedUnits;
QStringList m_includePaths;
unsigned int depresolv : 1;
Lexer *lexer;
SourceProvider* m_sourceProvider;
ParsedFilePointer m_currentParsedFile;
CachedLexedFilePointer m_currentLexerCache;
LexerCache m_lexerCache;
int m_dependenceDepth;
int m_maxDependenceDepth;
class ParseHelper;
friend class ParseHelper;
private:
Driver(const Driver& source);
void operator = (const Driver& source);
};
#endif
|