File indexing completed on 2024-04-28 16:57:38

0001 #!/usr/bin/env python2
0002 
0003 
0004 # This file is part of the clazy static checker.
0005 
0006 # Copyright (C) 2017 Sergio Martins <smartins@kde.org>
0007 
0008 # This library is free software; you can redistribute it and/or
0009 # modify it under the terms of the GNU Library General Public
0010 # License as published by the Free Software Foundation; either
0011 # version 2 of the License, or (at your option) any later version.
0012 
0013 # This library is distributed in the hope that it will be useful,
0014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016 # Library General Public License for more details.
0017 
0018 # You should have received a copy of the GNU Library General Public License
0019 # along with this library; see the file COPYING.LIB.  If not, write to
0020 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021 # Boston, MA 02110-1301, USA.
0022 
0023 # This script is useful for fixing the json database, in case there's compiler flags
0024 # you want to remove or add. One such example is when you're required to build your
0025 # project with Apple clang, which will then generate pch errors when running
0026 # clazy.
0027 
0028 import json, sys, string
0029 
0030 #-------------------------------------------------------------------------------
0031 # Filters. Configure here.
0032 
0033 # Removes arguments starting with -include
0034 def filter_no_pch(str):
0035     return not (str.startswith("-include") or str.startswith(".pch/"))
0036 
0037 def remove_pch(c_splitted):
0038     return filter(filter_no_pch, c_splitted)
0039 
0040 def fix_command(c):
0041     c_splitted = c.split()
0042     c_splitted = remove_pch(c_splitted)
0043     return string.join(c_splitted)
0044 
0045 
0046 def fix_arguments(args):
0047     args = remove_pch(args)
0048     return args
0049 
0050 #-------------------------------------------------------------------------------
0051 # Main:
0052 
0053 f = open(sys.argv[1], 'r')
0054 contents = f.read()
0055 f.close()
0056 
0057 decoded = json.loads(contents)
0058 new_decoded = []
0059 for cmd in decoded:
0060     if 'file' in cmd:
0061         if cmd['file'].endswith('.moc') or cmd['file'].endswith('.rcc') or cmd['file'].endswith('_moc.cpp'):
0062             continue
0063 
0064     if 'command' in cmd or 'arguments' in cmd:
0065         if 'command' in cmd:
0066             cmd['command'] = fix_command(cmd['command'])
0067         if 'arguments' in cmd:
0068             cmd['arguments'] = fix_arguments(cmd['arguments'])
0069 
0070         new_decoded.append(cmd)
0071 
0072 new_contents = json.dumps(new_decoded)
0073 print new_contents