Make grit resource maps generate maps for strings.

This allows mapping from "IDS_FOO" to IDS_FOO to
get generated string identifiers.

Patch by sigbjorn@opera.com.  Code review at
https://codereview.chromium.org/22739002/ .

R=tony

Review URL: https://codereview.chromium.org/23090002

git-svn-id: http://grit-i18n.googlecode.com/svn/trunk@133 7262f16d-afe8-6277-6482-052fa10e57b1
diff --git a/grit/format/resource_map.py b/grit/format/resource_map.py
index 90370aa..b5a7b45 100644
--- a/grit/format/resource_map.py
+++ b/grit/format/resource_map.py
@@ -104,17 +104,23 @@
 
 def _FormatSource(get_key, root, lang, output_dir):
   from grit.format import rc_header
-  from grit.node import include, structure
+  from grit.node import include, structure, message
   yield _FormatSourceHeader(root)
   tids = rc_header.GetIds(root)
   seen = set()
+  active_descendants = [item for item in root.ActiveDescendants()]
   for item in root:
-    if isinstance(item, (include.IncludeNode, structure.StructureNode)):
+    if isinstance(item, (include.IncludeNode,
+                         structure.StructureNode,
+                         message.MessageNode)):
       key = get_key(item)
       tid = item.attrs['name']
       if tid in tids and key not in seen:
         seen.add(key)
-        yield '  {"%s", %s},\n' % (key, tid)
+        # For messages, only include the active ones
+        if not isinstance(item, message.MessageNode) \
+            or item in active_descendants:
+          yield '  {"%s", %s},\n' % (key, tid)
   yield _FormatSourceFooter(root)
 
 
diff --git a/grit/format/resource_map_unittest.py b/grit/format/resource_map_unittest.py
index ea34465..6112e03 100644
--- a/grit/format/resource_map_unittest.py
+++ b/grit/format/resource_map_unittest.py
@@ -94,6 +94,63 @@
 };
 const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
 
+  def testFormatStringResourceMap(self):
+    grd = grd_reader.Parse(StringIO.StringIO(
+      '''<?xml version="1.0" encoding="UTF-8"?>
+      <grit latest_public_release="2" source_lang_id="en" current_release="3"
+            base_dir=".">
+        <outputs>
+          <output type="rc_header" filename="the_rc_header.h" />
+          <output type="resource_map_header" filename="the_rc_map_header.h" />
+          <output type="resource_map_source" filename="the_rc_map_source.cc" />
+        </outputs>
+        <release seq="1" allow_pseudo="false">
+          <messages fallback_to_english="true">
+            <message name="IDS_PRODUCT_NAME" desc="The application name">
+              Application
+            </message>
+            <if expr="1">
+              <message name="IDS_DEFAULT_TAB_TITLE_TITLE_CASE"
+                  desc="In Title Case: The default title in a tab.">
+                New Tab
+              </message>
+            </if>
+            <if expr="0">
+              <message name="IDS_DEFAULT_TAB_TITLE"
+                  desc="The default title in a tab.">
+                New tab
+              </message>
+            </if>
+          </messages>
+        </release>
+      </grit>'''), util.PathFromRoot('.'))
+    grd.SetOutputLanguage('en')
+    grd.RunGatherers()
+    output = util.StripBlankLinesAndComments(''.join(
+        resource_map.GetFormatter('resource_map_header')(grd, 'en', '.')))
+    self.assertEqual('''\
+#include <stddef.h>
+#ifndef GRIT_RESOURCE_MAP_STRUCT_
+#define GRIT_RESOURCE_MAP_STRUCT_
+struct GritResourceMap {
+  const char* const name;
+  int value;
+};
+#endif // GRIT_RESOURCE_MAP_STRUCT_
+extern const GritResourceMap kTheRcHeader[];
+extern const size_t kTheRcHeaderSize;''', output)
+    output = util.StripBlankLinesAndComments(''.join(
+        resource_map.GetFormatter('resource_map_source')(grd, 'en', '.')))
+    self.assertEqual('''\
+#include "the_rc_map_header.h"
+#include "base/basictypes.h"
+#include "the_rc_header.h"
+const GritResourceMap kTheRcHeader[] = {
+  {"IDS_PRODUCT_NAME", IDS_PRODUCT_NAME},
+  {"IDS_DEFAULT_TAB_TITLE_TITLE_CASE", IDS_DEFAULT_TAB_TITLE_TITLE_CASE},
+};
+const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
+
 
 if __name__ == '__main__':
   unittest.main()