#!/usr/bin/ruby
#
# stoat - LLVM Based Static Analysis Tool
# Copyright (C) 2015 Mark McCurry
#
# This file is part of stoat.
#
# stoat is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# stoat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with stoat.  If not, see <http://www.gnu.org/licenses/>.
#

#Pretend to be clang
NARGV = ARGV.collect {|x| x.gsub(/(?=\W)/, '\\')}
print `clang++ #{NARGV.join(' ')}`
err_code = $?.to_i
if(err_code != 0)
    exit(false)
end

modify_next = false
do_compile  = false
modified    = false
new_args = []
NARGV.each do |x|
    if(x  == "\\-c")
        do_compile = true
    end
    if(modify_next)
        modify_next = false
        modified = true
        x = x+".bc"
    elsif(x=="\\-o")
        modify_next = true
    elsif(x=="\\-O3")
        next
    elsif(x=="\\-O2")
        next
    end
    new_args << x
end
if(do_compile && modified)
    `clang++ -emit-llvm #{new_args.join(' ')} 2>/dev/null`
end
