aboutsummaryrefslogtreecommitdiff
path: root/src/kconfig_compiler/KConfigCodeGeneratorBase.h
diff options
context:
space:
mode:
authorTomaz Canabrava <tcanabrava@kde.org>2020-01-14 18:52:43 +0000
committerTomaz Canabrava <tcanabrava@kde.org>2020-01-16 10:15:36 +0000
commit98c32e29f50465d4d4e16bafdf0491edbfb422b0 (patch)
tree06898a9ff9a5ed2e385b25a6b7fe258bb9eacb9c /src/kconfig_compiler/KConfigCodeGeneratorBase.h
parenta601ac563d33681569befd22c9182cddaecf30b1 (diff)
downloadkconfig-98c32e29f50465d4d4e16bafdf0491edbfb422b0.tar.gz
kconfig-98c32e29f50465d4d4e16bafdf0491edbfb422b0.tar.bz2
WIP: Refactor KConfigXT
Summary: The current KConfigXT compiler is in a sad state: It's a massive file with loads of global variables that handle state, the generator is done within the main() function and it seems to have grown organically. There are no classes to separate logic / state / generation, what exists is code that generates code from a xml / ini pair, but it's hard to even discover what a bit of code is doing. The code istyle is C++ / Java from the nineties, which is not bad per see but it also uses quite a few things that are going to be deprecated in Qt 6 so I'm also taking the time make the code more streamlined with newer code style (no iterators, lambdas, auto usage, etc). The code that generates the files simplly pushes strings to a text stream, and it's hard to figure out when something starts or something ends: for instance, the code that generates the Constructor has more than sixty lines of code englobing some nested if - for - if - for constructs. Currently the code is "done" - there's one bug that I still need to find & fix regarding Translations, but the rest seems sane. The current testcode generates incorrect *whitespaces* regarding the old code (there's some parts that I feel that it's important to fix before merging, but overall, the whitespace changes are not bad and easier to handle, old code had a hand-counted amount of spaces before each line, new code has a function whitespace() that adds the current-and-correct amount of whitespaces based on indentation level that you start by startScope() and ends with endScope(). rest of the code still needs to be ported to it. I plan to fix the testcases whitespace by manually adding them, I'v fougth with the code for a while and added a few hacks there but I don't want to make the code hackish again. New code is not perfect by any means, but is a good step in the right direction. This code tries to Separate the compiler code into many different files / classes to be more obvious what's happening, and each class also has many helper methods to minimize copypaste. - CodeGenerator: Has base code for the header and source files that can be shared - HeaderGenerator: Logic for generating the header file - SourceGenerator: Logic for generating the source file - KcfgParser: Logic for parsing the kcfg file and extracting the information from the Xml file - CommonStructs: a header that contains the structs that are currently used everywhere. - KConfigParameters: (was CfgConfig - ConfigConfig, wat) - Has information passed via the kcfgc file - kcfg_compiler - will be renamed to main - start the other classes and generates the files. This code here currently has the begining of this separation, with the CodeGenerator and the HeaderGenerator in a ~good~ state, but unfinished. Test Plan: - Run the test cases, - Compare the diffs generated by the testcases and fix in the code the errors / differences - Run and compare real kde source with the new and old generators to look for errors Reviewers: #frameworks, ervin, bport, dfaure Reviewed By: dfaure Subscribers: bport, ngraham, kde-frameworks-devel Tags: #frameworks Differential Revision: https://phabricator.kde.org/D26202
Diffstat (limited to 'src/kconfig_compiler/KConfigCodeGeneratorBase.h')
-rw-r--r--src/kconfig_compiler/KConfigCodeGeneratorBase.h118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/kconfig_compiler/KConfigCodeGeneratorBase.h b/src/kconfig_compiler/KConfigCodeGeneratorBase.h
new file mode 100644
index 00000000..fdf3b7e3
--- /dev/null
+++ b/src/kconfig_compiler/KConfigCodeGeneratorBase.h
@@ -0,0 +1,118 @@
+/*
+ This file is part of KDE.
+
+ Copyright (C) 2020 Tomaz Cananbrava (tcanabrava@kde.org)
+ Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
+ Copyright (c) 2003 Waldo Bastian <bastian@kde.org>
+ Copyright (c) 2003 Zack Rusin <zack@kde.org>
+ Copyright (c) 2006 Michaƫl Larouche <michael.larouche@kdemail.net>
+ Copyright (c) 2008 Allen Winter <winter@kde.org>
+ Copyright (C) 2020 Tomaz Cananbrava (tcanabrava@kde.org)
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ 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.
+*/
+
+#ifndef KCONFIGCODEGENERATORBASE_H
+#define KCONFIGCODEGENERATORBASE_H
+
+#include <QString>
+#include <QTextStream>
+#include <QFile>
+#include <QVector>
+
+#include "KConfigXTParameters.h"
+
+class CfgEntry;
+struct ParseResult;
+
+/* This class manages the base of writing a C - Based code */
+class KConfigCodeGeneratorBase {
+public:
+ enum ScopeFinalizer {None, Semicolon};
+
+ KConfigCodeGeneratorBase(
+ const QString& inputFileName, // The kcfg file
+ const QString& baseDir, // where we should store the generated file
+ const QString& fileName, // the name of the generated file
+ const KConfigXTParameters &parameters, // parameters passed to the generator
+ ParseResult &parseResult // The pre processed configuration entries
+ );
+ virtual ~KConfigCodeGeneratorBase();
+
+ // iterates over the header list adding an #include directive.
+ void addHeaders(const QStringList& header);
+
+ // Create all the namespace indentation levels based on the parsed result and parameters */
+ void beginNamespaces();
+
+ // Closes all the namespaces adding lines with single '}'
+ void endNamespaces();
+
+ // Add the correct amount of whitespace in the code.
+ QString whitespace();
+
+ // start a block scope `{` and increase indentation level.
+ void endScope(ScopeFinalizer finalizer = None);
+
+ // end a block scope `}` and decrease indentation level.
+ void startScope();
+
+ // start writing to the output file
+ virtual void start();
+
+ // save the result on the disk
+ void save();
+
+ // Code Implementations
+ // Implements the `Get` methods for the CfgEntry
+ // TODO: write to the stream directly without returning a QString.
+ QString memberAccessorBody(const CfgEntry *e, bool globalEnums) const;
+
+ // Implements the `Set` methods for the CfgEntry
+ void memberMutatorBody(const CfgEntry *e);
+
+ // This is the code that creates the logic for the Setter / Mutator.
+ // It *just* creates the if test, no body. The reason is that just
+ // the if test was more than 20 lines of code and hard to understand
+ // what was happening in a bigger function.
+ void createIfSetLogic(const CfgEntry *e, const QString &varExpression);
+
+protected:
+ /* advance the number of spaces for the indentation level */
+ void indent();
+
+ /* reduce the number of spaces for the indentation level */
+ void unindent();
+
+ QString inputFile; // the base file name, input file is based on this.
+
+ QString baseDir; // Where we are going to save the file
+ QString fileName; // The file name
+
+ const KConfigXTParameters &cfg; // The parameters passed via the kcfgc file
+ ParseResult &parseResult; // the result of the parsed kcfg file
+ QTextStream stream; // the stream that operates in the file to write data.
+ QFile file; // The file handler.
+
+ // Special access to `this->` and `const` thru the code.
+ QString This;
+ QString Const;
+
+private:
+ int indentLevel = 0;
+};
+
+#endif