Improve detection of when forward declaration or #include is needed