A generalization of the Cheeger-Gromoll splitting theorem