As many as 400,000 nonprofit organizations are weeks away from a doomsday.

At midnight on May 15, an estimated one-fifth to one-quarter of some 1.6 million charities, trade associations and membership groups will lose their tax exemptions, thanks to a provision buried in a 2006 federal bill aimed at pension reform.

“It’s going to be an unholy mess once these organizations realize what’s happened to them,” said Diana Aviv, president of the Independent Sector, a nonprofit trade group.

The federal legislation passed in 2006 required all nonprofits to file tax forms the following year. Previously, only organizations with revenues of $25,000 or more — or the vast majority of nonprofit groups — had to file.

The new law, embedded in the 393 pages of the Pension Protection Act of 2006, also directed the Internal Revenue Service to revoke the tax exemptions of groups that failed to file for three consecutive years. Three years have passed, and thus the deadline looms.