summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@digia.com>2013-06-18 11:24:41 +0200
committerJoerg Bornemann <joerg.bornemann@digia.com>2013-06-18 15:07:00 +0200
commitf89cf14775ea620f9eaba39c7f0cdeb86c4cd117 (patch)
treeac648af7730bbd4c905e5f2f0831f47d1bd8cebf
parentcb3c669a1a92d8f81fd113f712fd596dc52b6a02 (diff)
downloadqbs-f89cf14775ea620f9eaba39c7f0cdeb86c4cd117.tar.gz
Check for multiple occurrences of items in SubProject item.
And potentially elsewhere too, if the Item::child() function is used. Change-Id: I4a5f53e6b1a0f0f49908ce1b8cd34ffce903c38c Reviewed-by: Joerg Bornemann <joerg.bornemann@digia.com>
-rw-r--r--src/lib/language/item.cpp23
-rw-r--r--src/lib/language/item.h2
-rw-r--r--src/lib/language/testdata/erroneous/multiple_properties_in_subproject.qbs8
-rw-r--r--src/lib/language/tst_language.cpp3
4 files changed, 30 insertions, 6 deletions
diff --git a/src/lib/language/item.cpp b/src/lib/language/item.cpp
index a2f1d0b85..85b4dac1f 100644
--- a/src/lib/language/item.cpp
+++ b/src/lib/language/item.cpp
@@ -30,6 +30,8 @@
#include "item.h"
#include "itempool.h"
#include "filecontext.h"
+#include <logging/translator.h>
+#include <tools/error.h>
#include <tools/qbsassert.h>
namespace qbs {
@@ -126,13 +128,24 @@ void Item::setPropertyObserver(ItemObserver *observer) const
m_propertyObserver = observer;
}
-Item *Item::child(const QString &type) const
+Item *Item::child(const QString &type, bool checkForMultiple) const
{
- foreach (Item * const child, children()) {
- if (child->typeName() == type)
- return child;
+ Item *child = 0;
+ foreach (Item * const currentChild, children()) {
+ if (currentChild->typeName() == type) {
+ if (!checkForMultiple)
+ return currentChild;
+ if (child) {
+ ErrorInfo error(Tr::tr("Multiple instances of item '%1' found where at most one "
+ "is allowed.").arg(type));
+ error.append(Tr::tr("First item"), child->location());
+ error.append(Tr::tr("Second item"), currentChild->location());
+ throw error;
+ }
+ child = currentChild;
+ }
}
- return 0;
+ return child;
}
} // namespace Internal
diff --git a/src/lib/language/item.h b/src/lib/language/item.h
index ae057455a..af59c0dbd 100644
--- a/src/lib/language/item.h
+++ b/src/lib/language/item.h
@@ -88,7 +88,7 @@ public:
Item *parent() const;
const FileContextPtr file() const;
QList<Item *> children() const;
- Item *child(const QString &type) const;
+ Item *child(const QString &type, bool checkForMultiple = true) const;
const PropertyMap &properties() const;
const PropertyDeclarationMap &propertyDeclarations() const;
const Modules &modules() const;
diff --git a/src/lib/language/testdata/erroneous/multiple_properties_in_subproject.qbs b/src/lib/language/testdata/erroneous/multiple_properties_in_subproject.qbs
new file mode 100644
index 000000000..0ecb41b34
--- /dev/null
+++ b/src/lib/language/testdata/erroneous/multiple_properties_in_subproject.qbs
@@ -0,0 +1,8 @@
+import qbs
+
+Project {
+ SubProject {
+ Properties { condition: false }
+ Properties { name: "blubb" }
+ }
+}
diff --git a/src/lib/language/tst_language.cpp b/src/lib/language/tst_language.cpp
index b5fa2aae2..25eb90a6d 100644
--- a/src/lib/language/tst_language.cpp
+++ b/src/lib/language/tst_language.cpp
@@ -283,6 +283,9 @@ void TestLanguage::erroneousFiles_data()
<< "Depends.submodules cannot be used if name contains a dot";
QTest::newRow("multiple_exports")
<< "Multiple Export items in one product are prohibited.";
+ QTest::newRow("multiple_properties_in_subproject")
+ << "Multiple instances of item 'Properties' found where at most one "
+ "is allowed.";
QTest::newRow("importloop1")
<< "Loop detected when importing";
QTest::newRow("nonexistentouter")