#!/usr/bin/python -tt

"""
This checks for 

#include "somedir/file.h"

and reports and error if the checked file is also in "somedir". It should read
corrected then

#include "file.h"
"""

def evaluate_matches(lines, fn):
    errors = []
    
    splitted_fn = fn.rsplit('/')
    if len(splitted_fn) == 1:
        return []
    
    for lineno,line in enumerate(lines):
        if not line.startswith("#include"):
            continue

        inc = line.split()[-1]
        if inc[0] != '"':
            continue
        inc = inc.strip('"')
       
        splitted_inc = inc.rsplit('/',1)
        if len(splitted_inc) == 1: # "file.h" not "dir/file.h"
            continue
        
        subdirdepth = splitted_inc[0].count('/') + 1

        dir = '/'.join(splitted_fn[-subdirdepth-1:-1])
         
        split_dir = splitted_inc[0]

        if split_dir == dir:
            errors.append( (fn, lineno+1, "Local include with subdir. Change from \"dir/file.h\" to \"file.h\""))
    return errors

# File this is called on is always called testdir/test.h

forbidden = [
    '#include "testdir/another_file.h"',
]

allowed = [
    '#include "file.h"',
    '#include "anotherdir/file.h"',
]
