Commit 7d24f921 authored by dt's avatar dt
Browse files

Merge branch '0.9.1-beta' of into 0.9.1-beta

parents a7227782 8e297ace
\contentpage{index.html}{Qt Creator}
\page coding-style.html
\title Qt Creator Coding Rules
\section1 Introduction
The aim of this section is to serve as a guide for the developers, to aid us
to build understandable and maintainable code, to create less confusion and
surprises when working on Qt Creator.
As usual: Rules are not set in stone. If there's a good reason to break one,
do it, preferably after making sure that there are others agreeing.
This document is incomplete.
In general, if you want to contribute to the main source, we expect at least
that you:
\list 1
\o The most important rule first: KISS (keep it simple ...): always
use a simple implementation in favor of a more complicated one.
This eases maintenance a lot.
\o Write good C++ code: Readable, well commented when necessary,
and taking advantage of the OO model. Follow the \l{Formatting} guidelines.
There are also certain \l{Code Constructs} that we try to follow.
\o Adapt the code to the structures already existing in Qt Creator, or in
the case that you have better ideas, discuss them with other developers
before writing the code.
\o Take advantage of Qt. Don't re-invent the wheel. Think about what parts
of your code are generic enough that they might be incorporated into
Qt proper.
\o Document interfaces. Right now we use qdoc, but changing to doxygen
is being considered.
\section1 Submitting Code
It is implicitly understood that all patches contributed to The Qt Creator
Project are made under under the Gnu General Public License, version 2 or later
If you have a problem with that, don't contribute code.
Also please don't just pop up out of the blue with a huge patch (or
small) that changes something substantial in Qt Creator. Always discuss your
ideas with the other developers on mailing list first.
When you create the patch, please use git or use "diff -up" since we find
that a lot easier to read than the other diff formats. Also please do not
send patches that implements or fixes several different things; several
patches is a much better option.
We also require you to provide a commit message entry with every patch,
this describes in detail what the patch is doing.
\section1 Code Constructs
We have several guidelines on code constructs, some of these exist to
make the code faster, others to make the code clearer. Yet others
exist to allow us to take advantage of the strong type checking
in C++.
\list 1
\o Declaration of variables should wait as long as possible. The rule
is: "Don't declare it until you need it." In C++ there are a lot of
user defined types, and these can very often be expensive to
initialize. This rule connects to the next rule too.
\o Make the scope of a variable as small as possible.
\o Prefer preincrement to postincrement whenever possible.
Preincrement has potential of being faster than postincrement. Just
think about the obvious implementations of pre/post-increment. This
rule applies to decrement too.
T++; // not used in Qt Creator
U--; // not used in Qt Creator
\o Try to minimize evaluation of the same code over and over. This is
aimed especially at loops.
Container::iterator end = large.end();
for (Container::iterator it = large.begin(); it != end; ++it) {
for (Container::iterator it = large.begin();
it != large.end(); ++it) {
\section1 Formatting
\section2 Declarations
Only one declaration on each line.
int a;
int b;
int a, b; // not used in Qt Creator
This is especially important when initialization is done at the same
QString a = "Joe";
QString b = "Foo";
QString a = "Joe", b = "Foo"; // not used in Qt Creator
[Note that 'QString a = "Joe"' is formally calling a copy constructor
on a temporary constructed from a string literal and therefore has the
potential of being more expensive then direct construction by
'QString a("joe")'. However the compiler is allowed to elide the copy
(even if it had side effects), and modern compilers typically do so.
Given these equal costs, Qt Creator code favours the '=' idiom as it is in
line with the traditional C-style initialization, _and_ cannot be
mistaken as function declaration, _and_ reduces the level of nested
parantheses in more initializations.]
\section2 Pointers and references
char *p = "flop";
char &c = *p;
char* p = "flop"; // not used in Qt Creator
char & c = *p; // not used in Qt Creator
This is simply in line with the official Qt guide lines.
Also note that we will have:
const char *p;
char const * p; // not used in Qt Creator
Using a plain 0 for Null pointer constants is always correct and least effort
to type. So:
void *p = 0;
void *p = NULL; // not used in Qt Creator
void *p = '\0'; // not used in Qt Creator
void *p = 42 - 7 * 6; // also not used in Qt Creator
Note: As an exception, imported third party code as well as code
interfacing the "native" APIs (src/support/os_*) can use NULL.
\section2 Operator names and parentheses
operator == (type) // not used in Qt Creator
The == is part of the function name, separating it makes the
declaration look like an expression.
\section2 Function names and parentheses
void mangle()
void mangle () // not used in Qt Creator
\section2 Naming rules
Simply follow the style of Qt proper. As examples:
\o Use descriptive but simple and short names. Do not abbreviate.
\o Class names are capitalized, and function names lowercased.
Enums are named like Classes, values are in lower-case.
\section2 Formatting
Adapt the formatting of your code to the one used in the
other parts of Qt Creator. In case there is different formatting for
the same construct, use the one used more often.
\section2 Declarations
- Use this order for the access sections of your class: public,
protected, private. The public section is interesting for every
user of the class. The private section is only of interest for the
implementors of the class (you). [Obviously not true since this is
for developers, and we do not want one developer only to be able to
read and understand the implementation of class internals. Lgb]
- Avoid declaring global objects in the declaration file of the class.
If the same variable is used for all objects, use a static member.
- Avoid global or static variables.
\section2 File headers
If you create a new file, the top of the file should include a
header comment equal to the one found in other source files of Qt Creator.
\section2 Documentation
The documentation is generated from source and header files.
You document for the other developers, not for yourself.
In the header you should document interfaces, i.e. what the function does,
not the implementation.
In the .cpp files you document the implementation if the implementation
in non-obvious.
......@@ -40,7 +40,7 @@
#include <QByteArray>
#include <QList>
#include <QSet>
#include <QMap>
#include <QSharedPointer>
#include <QString>
#include <QStringList>
......@@ -236,6 +236,16 @@ private:
QList<MacroUse> _macroUses;
class CPLUSPLUS_EXPORT Snapshot: public QMap<QString, Document::Ptr>
{ }
{ }
} // end of namespace CPlusPlus
......@@ -77,7 +77,7 @@ LookupContext::LookupContext(Control *control)
LookupContext::LookupContext(Symbol *symbol,
Document::Ptr expressionDocument,
Document::Ptr thisDocument,
const QMap<QString, Document::Ptr> &documents)
const Snapshot &documents)
: _symbol(symbol),
......@@ -57,7 +57,7 @@ public:
LookupContext(Symbol *symbol,
Document::Ptr expressionDocument,
Document::Ptr thisDocument,
const QMap<QString, Document::Ptr> &documents);
const Snapshot &documents);
LookupContext(Symbol *symbol,
const LookupContext &context);
......@@ -87,7 +87,7 @@ public:
QList<Symbol *> resolveClassOrNamespace(Name *name) const
{ return resolveClassOrNamespace(name, visibleScopes()); }
QMap<QString, Document::Ptr> documents() const
Snapshot snapshot() const
{ return _documents; }
enum ResolveMode {
......@@ -140,7 +140,7 @@ private:
Document::Ptr _thisDocument;
// All documents.
QMap<QString, Document::Ptr> _documents;
Snapshot _documents;
// Visible scopes.
QList<Scope *> _visibleScopes;
......@@ -46,9 +46,9 @@ TypeOfExpression::TypeOfExpression():
void TypeOfExpression::setDocuments(const QMap<QString, Document::Ptr> &documents)
void TypeOfExpression::setSnapshot(const Snapshot &documents)
m_documents = documents;
m_snapshot = documents;
m_lookupContext = LookupContext();
......@@ -59,12 +59,12 @@ QList<TypeOfExpression::Result> TypeOfExpression::operator()(const QString &expr
QString code = expression;
if (mode == Preprocess)
code = preprocessedExpression(expression, m_documents, document);
code = preprocessedExpression(expression, m_snapshot, document);
Document::Ptr expressionDoc = documentForExpression(code);
m_ast = extractExpressionAST(expressionDoc);
m_lookupContext = LookupContext(lastVisibleSymbol, expressionDoc,
document, m_documents);
document, m_snapshot);
ResolveExpression resolveExpression(m_lookupContext);
return resolveExpression(m_ast);
......@@ -103,10 +103,12 @@ Document::Ptr TypeOfExpression::documentForExpression(const QString &expression)
return doc;
void TypeOfExpression::processEnvironment(QMap<QString, Document::Ptr> documents,
void TypeOfExpression::processEnvironment(Snapshot documents,
Document::Ptr doc, Environment *env,
QSet<QString> *processed) const
if (! doc)
if (processed->contains(doc->fileName()))
......@@ -120,7 +122,7 @@ void TypeOfExpression::processEnvironment(QMap<QString, Document::Ptr> documents
QString TypeOfExpression::preprocessedExpression(const QString &expression,
QMap<QString, Document::Ptr> documents,
Snapshot documents,
Document::Ptr thisDocument) const
Environment env;
......@@ -61,7 +61,7 @@ public:
* Also clears the lookup context, so can be used to make sure references
* to the documents previously used are removed.
void setDocuments(const QMap<QString, Document::Ptr> &documents);
void setSnapshot(const Snapshot &documents);
enum PreprocessMode {
......@@ -100,15 +100,15 @@ private:
ExpressionAST *extractExpressionAST(Document::Ptr doc) const;
Document::Ptr documentForExpression(const QString &expression) const;
void processEnvironment(QMap<QString, CPlusPlus::Document::Ptr> documents,
void processEnvironment(CPlusPlus::Snapshot documents,
CPlusPlus::Document::Ptr doc, CPlusPlus::Environment *env,
QSet<QString> *processed) const;
QString preprocessedExpression(const QString &expression,
QMap<QString, CPlusPlus::Document::Ptr> documents,
CPlusPlus::Snapshot documents,
CPlusPlus::Document::Ptr thisDocument) const;
QMap<QString, Document::Ptr> m_documents;
Snapshot m_snapshot;
ExpressionAST *m_ast;
LookupContext m_lookupContext;
......@@ -46,11 +46,12 @@
#include <QtGui/QHBoxLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QToolButton>
#include <QtGui/QPushButton>
namespace Core {
namespace Utils {
#ifdef Q_OS_OSX
#ifdef Q_OS_MAC
/*static*/ const char * const PathChooser::browseButtonLabel = "Choose...";
/*static*/ const char * const PathChooser::browseButtonLabel = "Browse...";
......@@ -112,7 +113,11 @@ PathChooser::PathChooser(QWidget *parent) :
#ifdef Q_OS_MAC
QPushButton *browseButton = new QPushButton;
QToolButton *browseButton = new QToolButton;
connect(browseButton, SIGNAL(clicked()), this, SLOT(slotBrowse()));
......@@ -342,6 +342,10 @@ NavigationSubWidget::NavigationSubWidget(NavigationWidget *parentWidget)
m_navigationComboBox = new NavComboBox(this);
m_navigationWidget = 0;
#ifdef Q_OS_MAC
// this is to avoid ugly tool bar behavior
m_toolbar = new QToolBar(this);
m_toolbar->setContentsMargins(0, 0, 0, 0);
......@@ -72,7 +72,7 @@ VersionDialog::VersionDialog(QWidget *parent)
"Built on " __DATE__ " at " __TIME__ "<br />"
"Using revision %5<br/>"
"From revision %5<br/>"
......@@ -427,7 +427,9 @@ void CPPEditor::switchDeclarationDefinition()
if (!m_modelManager)
Document::Ptr doc = m_modelManager->document(file()->fileName());
const Snapshot snapshot = m_modelManager->snapshot();
Document::Ptr doc = snapshot.value(file()->fileName());
if (!doc)
Symbol *lastSymbol = doc->findSymbolAt(line, column);
......@@ -445,7 +447,7 @@ void CPPEditor::switchDeclarationDefinition()
if (f) {
TypeOfExpression typeOfExpression;
QList<TypeOfExpression::Result> resolvedSymbols = typeOfExpression(QString(), doc, lastSymbol);
const LookupContext &context = typeOfExpression.lookupContext();
......@@ -474,10 +476,12 @@ void CPPEditor::jumpToDefinition()
if (!m_modelManager)
const Snapshot snapshot = m_modelManager->snapshot();
// Find the last symbol up to the cursor position
int line = 0, column = 0;
convertPosition(position(), &line, &column);
Document::Ptr doc = m_modelManager->document(file()->fileName());
Document::Ptr doc = snapshot.value(file()->fileName());
if (!doc)
......@@ -503,7 +507,7 @@ void CPPEditor::jumpToDefinition()
// Evaluate the type of the expression
TypeOfExpression typeOfExpression;
QList<TypeOfExpression::Result> resolvedSymbols =
typeOfExpression(expression, doc, lastSymbol);
......@@ -572,7 +576,7 @@ Symbol *CPPEditor::findDefinition(Symbol *lastSymbol)
QualifiedNameId *q = control.qualifiedNameId(&qualifiedName[0], qualifiedName.size());
LookupContext context(&control);
const QMap<QString, Document::Ptr> documents = m_modelManager->documents();
const Snapshot documents = m_modelManager->snapshot();
foreach (Document::Ptr doc, documents) {
QList<Scope *> visibleScopes;
......@@ -744,7 +748,8 @@ void CPPEditor::unCommentSelection()
QString endText = endBlock.text();
int endPos = end - endBlock.position();
bool hasTrailingCharacters = !endText.mid(endPos).trimmed().isEmpty();
bool hasTrailingCharacters = !endText.left(endPos).remove(QLatin1String("//")).trimmed().isEmpty()
&& !endText.mid(endPos).trimmed().isEmpty();
if ((endPos <= endText.length() - 2
&& == QLatin1Char('*')
&& == QLatin1Char('/'))) {
......@@ -434,10 +434,12 @@ int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
//if (! expression.isEmpty())
//qDebug() << "***** expression:" << expression;
if (Document::Ptr thisDocument = m_manager->document(fileName)) {
const Snapshot snapshot = m_manager->snapshot();
if (Document::Ptr thisDocument = snapshot.value(fileName)) {
Symbol *symbol = thisDocument->findSymbolAt(line, column);
QList<TypeOfExpression::Result> resolvedTypes = typeOfExpression(expression, thisDocument, symbol,
......@@ -964,8 +966,10 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
if (Function *function = symbol->type()->asFunction()) {
// If the member is a function, automatically place the opening parenthesis,
// except when it might take template parameters.
if (!function->returnType().isValid()
&& (function->identity() && !function->identity()->isDestructorNameId())) {
const bool hasReturnType = function->returnType().isValid() ||
function->returnType().isSigned() ||
if (! hasReturnType && (function->identity() && !function->identity()->isDestructorNameId())) {
// Don't insert any magic, since the user might have just wanted to select the class
} else if (function->templateParameterCount() != 0) {
......@@ -1034,7 +1038,7 @@ void CppCodeCompletion::cleanup()
// Set empty map in order to avoid referencing old versions of the documents
// until the next completion
typeOfExpression.setDocuments(QMap<QString, Document::Ptr>());
int CppCodeCompletion::findStartOfName(const TextEditor::ITextEditor *editor)
......@@ -165,9 +165,11 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
QTextCursor tc(edit->document());
const Snapshot documents = m_manager->snapshot();
const int lineNumber = tc.block().blockNumber() + 1;
const QString fileName = editor->file()->fileName();
Document::Ptr doc = m_manager->document(fileName);
Document::Ptr doc = documents.value(fileName);
if (doc) {
foreach (Document::DiagnosticMessage m, doc->diagnosticMessages()) {
if (m.line() == lineNumber) {
......@@ -212,7 +214,7 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
Symbol *lastSymbol = doc->findSymbolAt(line, column);
TypeOfExpression typeOfExpression;
QList<TypeOfExpression::Result> types = typeOfExpression(expression, doc, lastSymbol);
if (!types.isEmpty()) {
......@@ -143,7 +143,7 @@ protected:
QPointer<CppModelManager> m_modelManager;
CppModelManager::DocumentTable m_documents;
Snapshot m_snapshot;
Environment env;
pp m_proc;
QStringList m_includePaths;
......@@ -160,7 +160,7 @@ private:
CppPreprocessor::CppPreprocessor(QPointer<CppModelManager> modelManager)
: m_modelManager(modelManager),
m_proc(this, env)
{ }
......@@ -340,7 +340,7 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc, QSet<QString> *process
foreach (QString includedFile, doc->includedFiles()) {
mergeEnvironment(m_documents.value(includedFile), processed);
mergeEnvironment(m_snapshot.value(includedFile), processed);
foreach (const Macro macro, doc->definedMacros()) {
......@@ -386,7 +386,7 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type,
if (! contents.isEmpty()) {
Document::Ptr cachedDoc = m_documents.value(fileName);
Document::Ptr cachedDoc = m_snapshot.value(fileName);
if (cachedDoc && m_currentDoc) {
} else {
......@@ -477,11 +477,8 @@ CppModelManager::CppModelManager(QObject *parent) :
{ }
Document::Ptr CppModelManager::document(const QString &fileName) const
{ return m_documents.value(fileName); }
CppModelManager::DocumentTable CppModelManager::documents() const
{ return m_documents; }
Snapshot CppModelManager::snapshot() const
{ return m_snapshot; }
void CppModelManager::ensureUpdated()
......@@ -672,7 +669,7 @@ void CppModelManager::emitDocumentUpdated(Document::Ptr doc)
void CppModelManager::onDocumentUpdated(Document::Ptr doc)
const QString fileName = doc->fileName();
m_documents[fileName] = doc;
m_snapshot[fileName] = doc;
QList<Core::IEditor *> openedEditors = m_core->editorManager()->openedEditors();
foreach (Core::IEditor *editor, openedEditors) {
if (editor->file()->fileName() == fileName) {
......@@ -837,7 +834,7 @@ void CppModelManager::parse(QFutureInterface<void> &future,
void CppModelManager::GC()
DocumentTable documents = m_documents;