From 68aeaa5fc40abd39ddd55f4530b9d6b4daaf95a8 Mon Sep 17 00:00:00 2001
From: Sergey Shambir <sergey.shambir.auto@gmail.com>
Date: Sun, 3 Feb 2013 20:47:45 +0400
Subject: [PATCH] Autotools: fixed parsing var assignment without space before
 =

Parser worked correctly with 'bin_PROGRAMS =', but failed on
'bin_PROGRAMS='. To reproduce problem on Ubuntu, perform 'apt-get source
gentoo' and open Makefile.am in downloaded folder as project: it will be
empty before this change.

Change-Id: I71d0866a2b9d8228ad5ac9237fa34be14b9e9b38
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: hjk <hjk121@nokiamail.com>
---
 .../makefileparser.cpp                        | 50 ++++++++++++-------
 .../autotoolsprojectmanager/makefileparser.h  |  6 +++
 2 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/src/plugins/autotoolsprojectmanager/makefileparser.cpp b/src/plugins/autotoolsprojectmanager/makefileparser.cpp
index 1f1dfdc299e..a6fc4c266d6 100644
--- a/src/plugins/autotoolsprojectmanager/makefileparser.cpp
+++ b/src/plugins/autotoolsprojectmanager/makefileparser.cpp
@@ -137,26 +137,27 @@ bool MakefileParser::isCanceled() const
 
 MakefileParser::TopTarget MakefileParser::topTarget() const
 {
-    TopTarget topTarget = Undefined;
-
     const QString line = m_line.simplified();
-    if (!line.isEmpty() && !line.startsWith(QLatin1Char('#'))) {
-        // TODO: Check how many fixed strings like AM_DEFAULT_SOURCE_EXT will
-        // be needed vs. variable strings like _SOURCES. Dependent on this a
-        // more clever way than this (expensive) if-cascading might be done.
-        if (line.startsWith(QLatin1String("AM_DEFAULT_SOURCE_EXT =")))
-            topTarget = AmDefaultSourceExt;
-        else if (line.startsWith(QLatin1String("bin_PROGRAMS =")))
-            topTarget = BinPrograms;
-        else if (line.startsWith(QLatin1String("BUILT_SOURCES =")))
-            topTarget = BuiltSources;
-        else if (line.contains(QLatin1String("SUBDIRS =")))
-            topTarget = SubDirs;
-        else if (line.contains(QLatin1String("_SOURCES =")))
-            topTarget = Sources;
-    }
 
-    return topTarget;
+    if (line.isEmpty() || line.startsWith(QLatin1Char('#')))
+        return Undefined;
+
+    const QString id = parseIdentifierBeforeAssign(line);
+    if (id.isEmpty())
+        return Undefined;
+
+    if (id == QLatin1String("AM_DEFAULT_SOURCE_EXT"))
+        return AmDefaultSourceExt;
+    if (id == QLatin1String("bin_PROGRAMS"))
+        return BinPrograms;
+    if (id == QLatin1String("BUILT_SOURCES"))
+        return BuiltSources;
+    if (id == QLatin1String("SUBDIRS") || id == QLatin1String("DIST_SUBDIRS"))
+        return SubDirs;
+    if (id.endsWith(QLatin1String("_SOURCES")))
+        return Sources;
+
+    return Undefined;
 }
 
 void MakefileParser::parseBinPrograms()
@@ -410,6 +411,19 @@ void MakefileParser::appendHeader(QStringList &list,  const QDir &dir, const QSt
     }
 }
 
+QString MakefileParser::parseIdentifierBeforeAssign(const QString &line)
+{
+    int end = 0;
+    for (; end < line.size(); ++end)
+        if (!line[end].isLetterOrNumber() && line[end] != QLatin1Char('_'))
+            break;
+
+    QString ret = line.left(end);
+    while (end < line.size() && line[end].isSpace())
+        ++end;
+    return (line[end] == QLatin1Char('=')) ? ret : QString();
+}
+
 void MakefileParser::addAllSources()
 {
     QStringList extensions;
diff --git a/src/plugins/autotoolsprojectmanager/makefileparser.h b/src/plugins/autotoolsprojectmanager/makefileparser.h
index e3b20a80744..c62ac2c6c39 100644
--- a/src/plugins/autotoolsprojectmanager/makefileparser.h
+++ b/src/plugins/autotoolsprojectmanager/makefileparser.h
@@ -205,6 +205,12 @@ private:
      */
     static void appendHeader(QStringList &list, const QDir &dir, const QString &fileName);
 
+    /**
+     * If line starts with identifier and = goes next, return identifier.
+     * Identifier is valid target name and it matches regexp [a-zA-Z1-9_]+
+     */
+    static QString parseIdentifierBeforeAssign(const QString &line);
+
 private:
     bool m_success;             ///< Return value for MakefileParser::parse().
 
-- 
GitLab