Allow obj-c import statements to be as long as necessary.

I've unified the checks for language specific exceptions to line length. There is a behavior change where cpp compiler directives no longer have a maximum line length (previously they were restricted to 150% of max line length). This change was made to match the behavior for java files.

BUG=NONE

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@238268 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py
index 816c784..5cbefcb 100644
--- a/presubmit_canned_checks.py
+++ b/presubmit_canned_checks.py
@@ -321,15 +321,28 @@
       'mk': 200,
       '': maxlen,
   }
-  # Note: these are C++ specific but processed on all languages. :(
-  MACROS = ('#define', '#include', '#import', '#pragma', '#if', '#endif')
 
-  # Special java statements.
-  SPECIAL_JAVA_STARTS = ('package ', 'import ')
+  # Language specific exceptions to max line length.
+  # '.h' is considered an obj-c file extension, since OBJC_EXCEPTIONS are a
+  # superset of CPP_EXCEPTIONS.
+  CPP_FILE_EXTS = ('c', 'cc')
+  CPP_EXCEPTIONS = ('#define', '#endif', '#if', '#include', '#pragma')
+  JAVA_FILE_EXTS = ('java',)
+  JAVA_EXCEPTIONS = ('import ', 'package ')
+  OBJC_FILE_EXTS = ('h', 'm', 'mm')
+  OBJC_EXCEPTIONS = ('#define', '#endif', '#if', '#import', '#include',
+                     '#pragma')
+
+  LANGUAGE_EXCEPTIONS = [
+    (CPP_FILE_EXTS, CPP_EXCEPTIONS),
+    (JAVA_FILE_EXTS, JAVA_EXCEPTIONS),
+    (OBJC_FILE_EXTS, OBJC_EXCEPTIONS),
+  ]
 
   def no_long_lines(file_extension, line):
-    # Allow special java statements to be as long as necessary.
-    if file_extension == 'java' and line.startswith(SPECIAL_JAVA_STARTS):
+    # Check for language specific exceptions.
+    if any(file_extension in exts and line.startswith(exceptions)
+           for exts, exceptions in LANGUAGE_EXCEPTIONS):
       return True
 
     file_maxlen = maxlens.get(file_extension, maxlens[''])
@@ -346,7 +359,6 @@
       return False
 
     return (
-        line.startswith(MACROS) or
         any((url in line) for url in ('http://', 'https://')) or
         input_api.re.match(
           r'.*[A-Za-z][A-Za-z_0-9]{%d,}.*' % long_symbol, line))
diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py
index 6bdc76f..3d2c75e 100755
--- a/tests/presubmit_unittest.py
+++ b/tests/presubmit_unittest.py
@@ -2071,6 +2071,12 @@
                      'importSomething ' + 'A ' * 50, 'foo.java',
                      presubmit.OutputApi.PresubmitPromptWarning)
 
+  def testCannedCheckObjCExceptionLongLines(self):
+    check = lambda x, y, _: presubmit_canned_checks.CheckLongLines(x, y, 80)
+    self.ContentTest(check, '#import ' + 'A ' * 150, 'foo.mm',
+                     'import' + 'A ' * 150, 'foo.mm',
+                     presubmit.OutputApi.PresubmitPromptWarning)
+
   def testCannedCheckMakefileLongLines(self):
     check = lambda x, y, _: presubmit_canned_checks.CheckLongLines(x, y, 80)
     self.ContentTest(check, 'A ' * 100, 'foo.mk', 'A ' * 100 + 'B', 'foo.mk',
@@ -2081,15 +2087,14 @@
     self.ContentTest(check, '012345678\n', None, '0123456789\n', None,
                      presubmit.OutputApi.PresubmitPromptWarning)
 
-  def testCannedCheckLongLinesMacro(self):
+  def testCannedCheckCppExceptionLongLines(self):
     check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z)
     self.ContentTest(
         check,
-        # Put a space in so it doesn't trigger long symbols. Allow 1/3 more.
-        '#if 56 89 12 45',
-        None,
-        '#if 56 89 12 456',
-        None,
+        '#if 56 89 12 45 9191919191919',
+        'foo.cc',
+        '#nif 56 89 12 45 9191919191919',
+        'foo.cc',
         presubmit.OutputApi.PresubmitPromptWarning)
 
   def testCannedCheckLongLinesHttp(self):